循环栅栏,用来进行线程协作,等待线程满足某个计数。构造时设置『计数个数』,每个线程执行到某个需要“同步”的时刻调用 await() 方法进行等待,当等待的线程数满足『计数个数』时,继续执行
功能和 countdownlatch 一样, 但是 CyclicBarrier 可以循环使用
private static void test01() { ExecutorService service = Executors.newFixedThreadPool(3); for (int i = 0; i < 5; i++) { CountDownLatch latch = new CountDownLatch(2); service.submit(() -> { log.debug("start ... "); latch.countDown(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new RuntimeException(e); } log.debug("end ... "); }); service.submit(() -> { log.debug("start ... "); latch.countDown(); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { throw new RuntimeException(e); } log.debug("end ... "); }); service.submit(() -> { log.debug("sum start ... "); try { latch.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } log.debug("sum end ... "); }); } service.shutdown(); }
可以看到上面的代码我们在每次循环的时候 , 都需要创建 CountDownLatch latch = new CountDownLatch(2);
public static void main(String[] args) { // test01(); ExecutorService service = Executors.newFixedThreadPool(2); CyclicBarrier barrier = new CyclicBarrier(2, () -> { log.debug("task1 , task2 finish ... "); }); for (int i = 0; i < 5; i++) { service.submit(() -> { log.debug("task1 start ... "); try { TimeUnit.SECONDS.sleep(1); barrier.await(); // 2 - 1 } catch (InterruptedException e) { throw new RuntimeException(e); } catch (BrokenBarrierException e) { throw new RuntimeException(e); } log.debug("end ... "); }); service.submit(() -> { log.debug("task2 start ... "); try { TimeUnit.SECONDS.sleep(2); barrier.await(); // 1 - 1 } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } log.debug("end ... "); }); } service.shutdown(); }
CyclicBarrier 相当于是人满发车, 比如 设置的大小是 2的时候, 只有执行 barrier.await()
的是2的时候, 线程才会向下执行,
14:18:57.402 [pool-1-thread-2] DEBUG c.CyclicBarrierTest - task2 start ... 14:18:57.402 [pool-1-thread-1] DEBUG c.CyclicBarrierTest - task1 start ... 14:18:59.417 [pool-1-thread-2] DEBUG c.CyclicBarrierTest - task1 , task2 finish ... 14:18:59.417 [pool-1-thread-2] DEBUG c.CyclicBarrierTest - end ... 14:18:59.417 [pool-1-thread-1] DEBUG c.CyclicBarrierTest - end ...
可以看到执行结果, task1 和 task2 等待的时间不同, 一个是1s, 一个是2s, 但是他们都是同时结束的
本文作者:王天赐
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!