CyclicBarrier测试案例一:
package com.dwz.utils; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; public class CyclicBarrierExample1 { public static void main(String[] args) { CyclicBarrier cyclicBarrier = new CyclicBarrier(3); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(20); System.out.println("t1 finished."); cyclicBarrier.await(); System.out.println("t1 The other thread finished too."); } catch (Exception e) { e.printStackTrace(); } }; }.start(); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(10); System.out.println("t2 finished."); cyclicBarrier.await(); System.out.println("t2 The other thread finished too."); } catch (Exception e) { e.printStackTrace(); } }; }.start(); try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } System.out.println("all of thread finished."); } }
结果:
t2 finished.
t1 finished.
all of thread finished.
t1 The other thread finished too.
t2 The other thread finished too.
测试案例二:
篱笆(cyclicBarrier)被破坏的时候给个回调通知
package com.dwz.utils; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; public class CyclicBarrierExample2 { public static void main(String[] args) { // 篱笆(cyclicBarrier)被破坏的时候给个回调通知 CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() { @Override public void run() { System.out.println("all of thread finished."); } }); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(20); System.out.println("t1 finished."); cyclicBarrier.await(); System.out.println("t1 The other thread finished too."); } catch (Exception e) { e.printStackTrace(); } }; }.start(); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(10); System.out.println("t2 finished."); cyclicBarrier.await(); System.out.println("t2 The other thread finished too."); } catch (Exception e) { e.printStackTrace(); } }; }.start(); } }
结果:
t2 finished.
t1 finished.
all of thread finished.
t1 The other thread finished too.
t2 The other thread finished too.
测试案例三:
cyclicBarrier.getNumberWaiting()不为0时会出现BrokenBarrierException
package com.dwz.utils; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; public class CyclicBarrierExample3 { public static void main(String[] args) throws InterruptedException { final CyclicBarrier cyclicBarrier = new CyclicBarrier(2); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(5); cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }; }.start(); new Thread() { public void run() { try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }; }.start(); TimeUnit.SECONDS.sleep(2); cyclicBarrier.reset(); } }
测试结果:
java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.dwz.utils.CyclicBarrierExample3$2.run(CyclicBarrierExample3.java:27)
测试案例四:
cyclicBarrier.reset()用法
package com.dwz.utils; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; public class CyclicBarrierExample4 { public static void main(String[] args) throws InterruptedException { final CyclicBarrier cyclicBarrier = new CyclicBarrier(3); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(5); cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }; }.start(); new Thread() { public void run() { try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }; }.start(); new Thread() { public void run() { try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }; }.start(); TimeUnit.SECONDS.sleep(4); System.out.println(cyclicBarrier.getNumberWaiting()); System.out.println(cyclicBarrier.getParties()); TimeUnit.SECONDS.sleep(2); System.out.println("================="); cyclicBarrier.reset(); System.out.println(cyclicBarrier.getNumberWaiting()); System.out.println(cyclicBarrier.getParties()); } }
测试结果:
2 3 ================= 0 3
测试案例五:
使用CountDownLatch实现CyclicBarrier的回调通知效果
package com.dwz.utils; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** * 使用CountDownLatch实现CyclicBarrier的回调通知效果 * * CountDownLatch 与 CyclicBarrier的区别 1.CountDownLatch不能reset,而CyclicBarrier是可以循环使用的 2.CountDownLatch的工作线程之间互不关心,而CyclicBarrier的工作线程必须等到同一个共同的点才去执行某个动作 */ public class CyclicBarrierExample5 { static class MyCountDownLatch extends CountDownLatch { private final Runnable runnable; public MyCountDownLatch(int count, Runnable runnable) { super(count); this.runnable = runnable; } @Override public void countDown() { super.countDown(); if(getCount() == 0) { this.runnable.run(); } } } public static void main(String[] args) { final MyCountDownLatch myCountDownLatch = new MyCountDownLatch(2, new Runnable() { @Override public void run() { System.out.println("all of work finish done."); } }); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } myCountDownLatch.countDown(); System.out.println(Thread.currentThread().getName() + " finished work."); }; }.start(); new Thread() { public void run() { try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } myCountDownLatch.countDown(); System.out.println(Thread.currentThread().getName() + " finished work."); }; }.start(); } }