标签:sys barrier div out tar dex thread public count
java.util.concurrent.CyclicBarrier是用来作为加法计数器的,和CountDownLatch用法类似
Demo如下:
public class CyclicBarrierDemo { public static void main(String[] args) { //计数到7之后,执行该线程 CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> { System.out.println("召唤皮卡丘成功!!!"); }); for (int i = 1; i <= 7; i++) { final int temp = i; new Thread(()->{ System.out.println(Thread.currentThread().getName() + "收集" + temp + "条皮卡"); try { //加一并等待 cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } },"小屁").start(); } } }
标签:sys barrier div out tar dex thread public count
原文地址:https://www.cnblogs.com/smallVampire/p/12931279.html