前言
这3个组件具有相似的用途
CountDownLatch倒数锁
await()方法的线程会被挂起,它会等待直到count值为0才继续执行,也提供有超时时间的await()方法,当超过这个时间还在等待会抛出异常
countDown()将count值减1
import java.util.concurrent.CountDownLatch;
public class Test16 extends Thread{
//如果把3改为4,可以看到3个线程都等待在那里了
public static CountDownLatch countDownLatch = new CountDownLatch(3);
public void run(){
System.out.println("thread"+Thread.currentThread().getName()+"wait");
//countDownLatch-1,减少一个倒数
countDownLatch.countDown();
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread"+Thread.currentThread().getName()+"again");
}
public static void main(String[] args) {
Test16 test1 = new Test16();
Test16 test2 = new Test16();
Test16 test3 = new Test16();
test1.setName("test1");
test2.setName("test2");
test3.setName("test3");
test1.start();
test2.start();
test3.start();
}
}
CyclicBarrier循环屏障
类似于CountDownLatch的复用版本,可以重置屏障进行再次多个线程的等待
await()方法的线程告诉CyclicBarrier自己已经到达同步点,然后当前线程被阻塞。直到n(设定好)个参与线程调用了await方法,同样提供带超时时间的await和不带超时时间的await方法
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Test17 extends Thread{
public static CyclicBarrier cyclicBarrier = new CyclicBarrier (3);
public void run(){
System.out.println("thread"+Thread.currentThread().getName()+"start");
try {
cyclicBarrier.await();
} catch (BrokenBarrierException | InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread"+Thread.currentThread().getName()+"again");
}
public static void main(String[] args) {
Test17 test1 = new Test17();
Test17 test2 = new Test17();
Test17 test3 = new Test17();
test1.setName("test1");
test2.setName("test2");
test3.setName("test3");
test1.start();
test2.start();
test3.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//重置了屏障,又需要3次等待
System.out.println("准备开始下一轮");
cyclicBarrier.reset();
Test17 test4 = new Test17();
Test17 test5 = new Test17();
Test17 test6 = new Test17();
test4.setName("test4");
test5.setName("test5");
test6.setName("test6");
test4.start();
test5.start();
test6.start();
}
}
Semaphore信号量
void acquire():从此信号量获取一个许可,在提供一个许可前一直将线程阻塞,否则线程被中断。
void release():释放一个许可,将其返回给信号量。
int availablePermits():返回此信号量中当前可用的许可数。
boolean hasQueuedThreads():查询是否有线程正在等待获取。
import java.util.concurrent.Semaphore;
public class Test18 extends Thread{
public static Semaphore semaphore = new Semaphore(3);
public void run(){
while (true){
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread"+Thread.currentThread().getName()+"acquire begin");
System.out.println("thread"+Thread.currentThread().getName()+"release");
semaphore.release();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Test18 test1 = new Test18();
Test18 test2 = new Test18();
Test18 test3 = new Test18();
Test18 test4 = new Test18();
Test18 test5 = new Test18();
Test18 test6 = new Test18();
test4.setName("test4");
test5.setName("test5");
test6.setName("test6");
test1.setName("test1");
test2.setName("test2");
test3.setName("test3");
test1.start();
test2.start();
test3.start();
test4.start();
test5.start();
test6.start();
}
}