Donnie

不积跬步无以至千里

CyclicBarrier Demo

    public class CyclicBarrierDemo {
    
        public static void main(String[] args) {
    
            //CyclicBarrier循环屏障,未达到屏障数需要等待一起执行
            CyclicBarrier cb= new CyclicBarrier(2);
    
            new Thread(()-> {
                try {
                    cb.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("e1:"+Thread.currentThread());
            },"t1").start();
    
           new Thread(()->{
    
               try {
                   TimeUnit.SECONDS.sleep(5);
                   cb.await();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               } catch (BrokenBarrierException e) {
                   e.printStackTrace();
               }
               System.out.println("e2:"+Thread.currentThread());
           },"t2").start();
    
    
        }
    }
赞赏支持