countDownLatch和cyclicbarrier的区别
- 2020-11-09 21:50:00
- admin 原创
- 1796
countDownLatch和cyclicbarrier的区别,请看下面的代码及注释
public void concurent() throws InterruptedException { System.out.println("主线程执行开始"); CountDownLatch cdl=new CountDownLatch(5); CyclicBarrier cyclicBarrier=new CyclicBarrier(5); ExecutorService es= Executors.newFixedThreadPool(5); for(int i=0;i<5;i++){ es.execute(()->{ try{ System.out.println("子线程:"+Thread.currentThread().getName()+"执行"); //等待5个线程一起执行,进行加操作,满足5个一起执行,执行之后从0开始重新计数 cyclicBarrier.await(); }catch (Exception e){ e.printStackTrace(); }finally { //进行减操作 cdl.countDown(); } }); } //进行线程等待,减到0的时候,进行释放 cdl.wait(); System.out.println("主线程执行结束"); }
发表评论
文章分类
联系方式
联系人: | 郑州-小万 |
---|---|
电话: | 13803993919 |
Email: | 1027060531@qq.com |
QQ: | 1027060531 |
网址: | www.wanhejia.com |
Update Required
To play the media you will need to either update your browser to a recent version or update your Flash plugin.