JUC辅助类之CyclicBarrier
作者:互联网
想实现收集七颗龙珠,召唤神龙的过程
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierDemo {
//创建固定值
private static final int NUMBER=7;
public static void main(String[] args) {
//创建CyclicBarrier
CyclicBarrier cyclicBarrier
=new CyclicBarrier(NUMBER,()->{
System.out.println("可以召唤神龙了");
System.out.println("\n");
});
for(int i=1;i<=7;i++){
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+"星龙珠收齐");
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
其与coundtdownLatch不同在于,可以通过reset方法重新计数,不同于CountDownLatch基于AQS,它底层是通过锁和Condition实现,本质上是volatile和CAS
标签:JUC,辅助,int,System,NUMBER,println,CyclicBarrier,out 来源: https://www.cnblogs.com/swifties270/p/16124991.html