java高并发系列 - 第17天:JUC中的循环栅栏CyclicBarrier常见的6种使用场景及代码示例 (3)

代码中创建CyclicBarrier对象时,多传入了一个参数(内部是倒酒操作),先到的人先等待,待所有人都到齐之后,需要先给大家倒酒,然后唤醒所有等待中的人让大家开饭。从输出结果中我们发现,倒酒操作是由最后一个人操作的,最后一个人倒酒完毕之后,才唤醒所有等待中的其他员工,让大家开饭。

示例4:其中一个人等待中被打断了

员工5等待中,突然接了个电话,有点急事,然后就拿起筷子开吃了,其他人会怎么样呢?看着他吃么?

代码如下:

package com.itsoku.chat15; import java.sql.Time; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.TimeUnit; /** * 微信公众号:javacode2018,获取年薪50万java课程 */ public class Demo4 { public static CyclicBarrier cyclicBarrier = new CyclicBarrier(10); public static class T extends Thread { int sleep; public T(String name, int sleep) { super(name); this.sleep = sleep; } @Override public void run() { long starTime = 0, endTime = 0; try { //模拟休眠 TimeUnit.SECONDS.sleep(sleep); starTime = System.currentTimeMillis(); //调用await()的时候,当前线程将会被阻塞,需要等待其他员工都到达await了才能继续 System.out.println(this.getName() + "到了!"); cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } endTime = System.currentTimeMillis(); System.out.println(this.getName() + ",sleep:" + this.sleep + " 等待了" + (endTime - starTime) + "(ms),开始吃饭了!"); } } public static void main(String[] args) throws InterruptedException { for (int i = 1; i <= 10; i++) { int sleep = 0; if (i == 10) { sleep = 10; } T t = new T("员工" + i, sleep); t.start(); if (i == 5) { //模拟员工5接了个电话,将自己等待吃饭给打断了 TimeUnit.SECONDS.sleep(1); System.out.println(t.getName() + ",有点急事,我先开干了!"); t.interrupt(); TimeUnit.SECONDS.sleep(2); } } } }

输出:

员工4到了! 员工3到了! 员工5到了! 员工1到了! 员工2到了! 员工5,有点急事,我先开干了! java.util.concurrent.BrokenBarrierException 员工1,sleep:0 等待了1001(ms),开始吃饭了! at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250) 员工3,sleep:0 等待了1001(ms),开始吃饭了! at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) 员工4,sleep:0 等待了1001(ms),开始吃饭了! at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) 员工2,sleep:0 等待了1001(ms),开始吃饭了! 员工5,sleep:0 等待了1002(ms),开始吃饭了! java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2048) at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:234) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) java.util.concurrent.BrokenBarrierException 员工6到了! at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) 员工9到了! at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) 员工8到了! 员工7到了! 员工6,sleep:0 等待了0(ms),开始吃饭了! 员工7,sleep:0 等待了1(ms),开始吃饭了! java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) 员工8,sleep:0 等待了1(ms),开始吃饭了! 员工9,sleep:0 等待了1(ms),开始吃饭了! Disconnected from the target VM, address: '127.0.0.1:64413', transport: 'socket' java.util.concurrent.BrokenBarrierException at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207) at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362) at com.itsoku.chat15.Demo4$T.run(Demo4.java:31) 员工10到了! 员工10,sleep:10 等待了0(ms),开始吃饭了!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzzwwd.html