Java并发-类库新组件 - CountDownLatch 理解
2016-02-25 19:28
447 查看
</pre><pre name="code" class="java">
直接附加上练习代码
package com.xyw.concurrent.blog; import java.util.concurrent.*; import java.util.*; /* * CountDownLatch 是进行同步一个或者多个任务,强制他们等待由其他任务执行的一组操作完成。 * 其有个初始的计数值,wait 的值都将被阻塞,直到计数值达到零的情况。 并且CountDownLatch 只能进行一次触发。计数器不能被重置。 * 如果被重置,请看下一期的讲解的情况。 */ class TaskPortion implements Runnable{ private static int counter = 0; private final int id = counter++; // 进行设置自增的ID 的选项 private static Random rand = new Random(47); private final CountDownLatch latch; TaskPortion(CountDownLatch latch){ this.latch = latch; } public void run(){ try{ doWork(); latch.countDown(); }catch(InterruptedException ex){ } } public void doWork() throws InterruptedException{ TimeUnit.MILLISECONDS.sleep(rand.nextInt(2000)); System.out.println(this + "completed!"); } public String toString(){ return String.format("%1$-3d", id); } }// 这是个进行分开执行的任务, performs some portion of a task class WaitingTask implements Runnable{ private static int counter = 0; private final int id = counter++; private final CountDownLatch latch; WaitingTask(CountDownLatch latch){ this.latch = latch; } public void run(){ try{ latch.await(); // 进行等待,知道 计数值为 零的时候,发生其中的停止的情况 System.out.println("Latch barrier passed for " + this); //System.out.println(latch.getCount()); // 这个是用来进行监视,是不是其中运行的关键值得情况。 }catch(InterruptedException e){ System.out.println(this + " interrupted"); } } public String toString(){ return String.format("WaitingTask %1$-3d", id); } } public class CountDownLatchDemo { static final int SIZE = 10; // 你可以进行改变 SIZE 的值来进行了解 CountDownLatch 类的原理的情况 public static void main(String[] args) throws Exception{ ExecutorService exec = Executors.newCachedThreadPool(); CountDownLatch latch = new CountDownLatch(SIZE); for(int i = 0; i< 100; i++){ exec.execute(new WaitingTask(latch)); } for(int i = 0; i < SIZE; i++){ exec.execute(new TaskPortion(latch)); // 这些都是线程的进行的工作,并且只有 // 其中的情况,等于一百的时候, WaitingTask 才进行运行,注意其中线程的先后的关系不是唯一确定的情况。 } System.out.println("Launched all tasks"); exec.shutdown(); // 当所有任务结束时完成 System.out.print(latch.getCount()+ "\n"); } }
// 运行结果
<pre name="code" class="java">Launched all tasks 10 7 completed! 9 completed! 5 completed! 8 completed! 1 completed! 2 completed! 6 completed! 4 completed! 0 completed! 3 completed! Latch barrier passed for WaitingTask 6 Latch barrier passed for WaitingTask 2 Latch barrier passed for WaitingTask 9 Latch barrier passed for WaitingTask 0 Latch barrier passed for WaitingTask 4 Latch barrier passed for WaitingTask 5 Latch barrier passed for WaitingTask 8 Latch barrier passed for WaitingTask 3 Latch barrier passed for WaitingTask 7 Latch barrier passed for WaitingTask 1
相关文章推荐
- 开源 免费 java CMS - FreeCMS2.1 会员站内信
- Eclipse中java文件生成jar文件的方法
- 阿里云服务器云数据库免费体验(Java Web详细实例)
- 阿里云服务器云数据库免费体验(Java Web详细实例)
- 阿里云服务器云数据库免费体验(Java Web详细实例)
- 阿里云服务器云数据库免费体验(Java Web详细实例)
- 整数转换成罗马数字java实现
- Spring 4.2.4.RELEASE MVC 学习笔记 - 4 - FreeMarker(咋个办呢 zgbn)
- JAVA设计模式--工厂方法模式
- 【JAVA知识】反射和注解(1)
- JAVA_接口演示_InterfaceDeamo.java____接口(interface)、默认(default)
- JAVA_接口演示_InterfaceDeamo.java____接口(interface)、默认(default)
- 六:ZooKeeper的java客户端api的使用
- 性能观察: Trove 集合类
- java项目中使用Sigar获取系统信息
- spring框架获取原型对象
- Spring整合JUnit框架进行单元测试代码使用详解
- Spring中@Autowired注解、@Resource注解的区别
- Spring 4.2.4.RELEASE MVC 学习笔记 - 3(咋个办呢 zgbn)
- struts原理