原创转载请注明出处:http://agilestyle.iteye.com/blog/2343117
tryAcquire()
无参方法tryAcquire()的作用是尝试获取1个permits,如果获取不到则返回false,此方法通常与if语句结合使用,具有无阻塞的特点。
Service.java
package org.fool.java.concurrent.semaphore.tryacquire;
import java.util.concurrent.Semaphore;
public class Service {
private Semaphore semaphore = new Semaphore(1);
public void testMethod() {
if(semaphore.tryAcquire()) {
System.out.println(Thread.currentThread().getName() + " invoked...");
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String str = new String();
Math.random();
}
semaphore.release();
} else {
System.out.println(Thread.currentThread().getName() + " not invoked...");
}
}
}
ThreadA.java
package org.fool.java.concurrent.semaphore.tryacquire;
public class ThreadA implements Runnable {
private Service service;
public ThreadA(Service service) {
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
TheadB.java
package org.fool.java.concurrent.semaphore.tryacquire;
public class ThreadB implements Runnable {
private Service service;
public ThreadB(Service service) {
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
TryAcquireTest.java
package org.fool.java.concurrent.semaphore.tryacquire;
public class TryAcquireTest {
public static void main(String[] args) {
Service service = new Service();
Thread a = new Thread(new ThreadA(service));
a.setName("A");
a.start();
Thread b = new Thread(new ThreadB(service));
b.setName("B");
b.start();
}
}
Run
tryAcquire(int permits)
有参方法tryAcquire(int permits)的作用是尝试获取x个permits,如果获取不到则返回false。
Service.java
package org.fool.java.concurrent.semaphore.tryacquire_1;
import java.util.concurrent.Semaphore;
public class Service {
private Semaphore semaphore = new Semaphore(3);
public void testMethod() {
if(semaphore.tryAcquire(3)) {
System.out.println(Thread.currentThread().getName() + " invoked...");
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String str = new String();
Math.random();
}
semaphore.release(3);
} else {
System.out.println(Thread.currentThread().getName() + " not invoked...");
}
}
}
ThreadA.java
package org.fool.java.concurrent.semaphore.tryacquire_1;
public class ThreadA implements Runnable {
private Service service;
public ThreadA(Service service) {
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
ThreadB.java
package org.fool.java.concurrent.semaphore.tryacquire_1;
public class ThreadB implements Runnable {
private Service service;
public ThreadB(Service service) {
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
ThreadC.java
package org.fool.java.concurrent.semaphore.tryacquire_1;
public class ThreadC implements Runnable {
private Service service;
public ThreadC(Service service) {
this.service = service;
}
@Override
public void run() {
service.testMethod();
}
}
TryAcquireTest2.java
package org.fool.java.concurrent.semaphore.tryacquire_1;
public class TryAcquireTest2 {
public static void main(String[] args) {
Service service = new Service();
Thread a = new Thread(new ThreadA(service));
a.setName("A");
a.start();
Thread b = new Thread(new ThreadB(service));
b.setName("B");
b.start();
Thread c = new Thread(new ThreadC(service));
c.setName("C");
c.start();
}
}
Run
tryAcquire(long timeout, TimeUnit unit)
有参方法tryAcquire(long timeout, TimeUnit unit)的作用是在指定的时间内尝试获取1个permits,如果获取不到则返回false。
修改Service.java
package org.fool.java.concurrent.semaphore.tryacquire_2;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class Service {
private Semaphore semaphore = new Semaphore(1);
public void testMethod() {
try {
if(semaphore.tryAcquire(3, TimeUnit.SECONDS)) {
System.out.println(Thread.currentThread().getName() + " invoked...");
for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
new String();
Math.random();
}
semaphore.release();
} else {
System.out.println(Thread.currentThread().getName() + " not invoked...");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
TryAcquireTest3.java
package org.fool.java.concurrent.semaphore.tryacquire_2;
public class TryAcquireTest3 {
public static void main(String[] args) {
Service service = new Service();
Thread a = new Thread(new ThreadA(service));
a.setName("A");
a.start();
Thread b = new Thread(new ThreadB(service));
b.setName("B");
b.start();
Thread c = new Thread(new ThreadC(service));
c.setName("C");
c.start();
}
}
Run
修改Service.java,注释掉for循环中的两行
for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
//new String();
//Math.random();
}
再Run
tryAcquire(int permits, long timeout, TimeUnit unit)
有参方法tryAcquire(int permits, long timeout, TimeUnit unit)的作用是在指定的时间内尝试获取x个permits,如果获取不到则返回false。
修改Service.java
package org.fool.java.concurrent.semaphore.tryacquire_3;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class Service {
private Semaphore semaphore = new Semaphore(3);
public void testMethod() {
try {
if(semaphore.tryAcquire(3, 3, TimeUnit.SECONDS)) {
System.out.println(Thread.currentThread().getName() + " invoked...");
for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
new String();
Math.random();
}
semaphore.release(3);
} else {
System.out.println(Thread.currentThread().getName() + " not invoked...");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
TryAcquireTest4.java
package org.fool.java.concurrent.semaphore.tryacquire_3;
public class TryAcquireTest4 {
public static void main(String[] args) {
Service service = new Service();
Thread a = new Thread(new ThreadA(service));
a.setName("A");
a.start();
Thread b = new Thread(new ThreadB(service));
b.setName("B");
b.start();
Thread c = new Thread(new ThreadC(service));
c.setName("C");
c.start();
}
}
Run
修改Service.java,注释掉for循环中的两行
for (int i = 0; i < Integer.MAX_VALUE / 10; i++) {
//new String();
//Math.random();
}
再Run