标签:oid rac art 接口 计数器 exce test catch col
CountDownLatch
每次当线程调用countDownLatch.countDown()方法时,会对计数器减1,减到0,countDownLatch.await()放行
public class CountDownLatchTest { public static void main(String[] args) { CountDownLatch count = new CountDownLatch(7); for (int i=0; i < 7; i++){ final int tempInt = i; new Thread(()->{ System.out.println("线程"+tempInt+"执行"); count.countDown(); },String.valueOf(i)).start(); } try { count.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("主线程开始执行"); } }
CyclicBarrier
与上面相反,当await()累计达到n次时,执行runnable 接口的线程。
public class CyclicBarrierTest { public static void main(String[] args) { CyclicBarrier cyc = new CyclicBarrier(7,()->{ System.out.println("一切就绪"); }); for (int i=0; i < 7; i++){ final int tempInt = i; new Thread(()->{ System.out.println("线程"+tempInt+"执行"); try { cyc.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } },String.valueOf(i)).start(); } } }
CountDownLatch 和 CyclicBarrier
标签:oid rac art 接口 计数器 exce test catch col
原文地址:https://www.cnblogs.com/wsZzz1997/p/14673466.html