线程同步工具之CountDownLatch示例
来源:互联网 发布:双色球破解软件 编辑:程序博客网 时间:2024/05/19 23:19
CountDownLatch的一个非常典型的应用场景是:有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行。假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减到0为止。
- package chapter3;
- import java.util.concurrent.CountDownLatch;
- public class Videoconference implements Runnable{
- private final CountDownLatch controller;
- public Videoconference(int number){
- controller = new CountDownLatch(number);
- }
- public void arrive(String name){
- System.out.println(name+” has arrived.”);
- controller.countDown();
- System.out.println(”VideoConference:Waiting for ”+controller.getCount());
- }
- @Override
- public void run() {
- System.out.println(”VideoConference:Initialization:”+controller.getCount());
- try {
- controller.await();
- System.out.printf(”VideoConference: All the participants have come\n”);
- System.out.printf(”VideoConference: Let’s start…\n”);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
package chapter3;import java.util.concurrent.CountDownLatch;public class Videoconference implements Runnable{ private final CountDownLatch controller; public Videoconference(int number){ controller = new CountDownLatch(number); } public void arrive(String name){ System.out.println(name+" has arrived."); controller.countDown(); System.out.println("VideoConference:Waiting for "+controller.getCount()); } @Override public void run() { System.out.println("VideoConference:Initialization:"+controller.getCount()); try { controller.await(); System.out.printf("VideoConference: All the participants have come\n"); System.out.printf("VideoConference: Let's start...\n"); } catch (InterruptedException e) { e.printStackTrace(); } }}
- package chapter3;
- import java.util.concurrent.TimeUnit;
- public class Participant implements Runnable{
- private Videoconference conference;
- private String name;
- public Participant(Videoconference conference,String name){
- this.conference = conference;
- this.name = name;
- }
- @Override
- public void run() {
- long duration = (long)(Math.random()*10);
- try {
- TimeUnit.SECONDS.sleep(duration);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- conference.arrive(name);
- }
- }
package chapter3;import java.util.concurrent.TimeUnit;public class Participant implements Runnable{ private Videoconference conference; private String name; public Participant(Videoconference conference,String name){ this.conference = conference; this.name = name; } @Override public void run() { long duration = (long)(Math.random()*10); try { TimeUnit.SECONDS.sleep(duration); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } conference.arrive(name); }}
- package chapter3;
- /**
- *
- * <p>
- * Description: CountDownLatch的学习
- * </p>
- * @author zhangjunshuai
- * @version 1.0
- * Create Date: 2014-9-25 下午8:11:55
- * Project Name: Java7Thread
- *
- * <pre>
- * Modification History:
- * Date Author Version Description
- * ———————————————————————————————————–
- * LastChange: Date:: Author: Rev:
- * </pre>
- *
- */
- public class Main2 {
- /**
- * <p>
- * </p>
- * @author zhangjunshuai
- * @date 2014-9-25 下午8:11:50
- * @param args
- */
- public static void main(String[] args) {
- Videoconference conference = new Videoconference(9);
- Thread threadConference = new Thread(conference);
- threadConference.start();
- for(int i=0;i<10;i++){
- Participant p = new Participant(conference, “Participant”+i);
- Thread t = new Thread(p);
- t.start();
- }
- }
- }
package chapter3;/** * * <p> * Description: CountDownLatch的学习 * </p> * @author zhangjunshuai * @version 1.0 * Create Date: 2014-9-25 下午8:11:55 * Project Name: Java7Thread * * <pre> * Modification History: * Date Author Version Description * ----------------------------------------------------------------------------------------------------------- * LastChange: $Date:: $ $Author: $ $Rev: $ * </pre> * */public class Main2 { /** * <p> * </p> * @author zhangjunshuai * @date 2014-9-25 下午8:11:50 * @param args */ public static void main(String[] args) { Videoconference conference = new Videoconference(9); Thread threadConference = new Thread(conference); threadConference.start(); for(int i=0;i<10;i++){ Participant p = new Participant(conference, "Participant"+i); Thread t = new Thread(p); t.start(); } }}
CountDownLatch类有3个基本元素:
- 初始值决定CountDownLatch类需要等待的事件的数量。
- await() 方法, 被等待全部事件终结的线程调用。
- countDown() 方法,事件在结束执行后调用。
当创建 CountDownLatch 对象时,对象使用构造函数的参数来初始化内部计数器。每次调用 countDown() 方法, CountDownLatch 对象内部计数器减一。当内部计数器达到0时, CountDownLatch 对象唤醒全部使用 await() 方法睡眠的线程们。
不可能重新初始化或者修改CountDownLatch对象的内部计数器的值。一旦计数器的值初始后,唯一可以修改它的方法就是之前用的 countDown() 方法。当计数器到达0时, 全部调用 await() 方法会立刻返回,接下来任何countDown() 方法的调用都将不会造成任何影响。
此方法与其他同步方法有这些不同:
CountDownLatch 机制不是用来保护共享资源或者临界区。它是用来同步一个或者多个执行多个任务的线程。它只能使用一次。像之前解说的,一旦CountDownLatch的计数器到达0,任何对它的方法的调用都是无效的。如果你想再次同步,你必须创建新的对象。
CountDownLatch 类有另一种版本的 await() 方法,它是:
- await(long time, TimeUnit unit): 此方法会休眠直到被中断; CountDownLatch 内部计数器到达0或者特定的时间过去了。TimeUnit 类包含了:DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, 和 SECONDS.
- 线程同步工具之CountDownLatch示例
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- 线程同步工具之CountDownLatch
- 线程同步工具-CountDownLatch
- 线程同步之CountDownLatch
- 12____java线程同步工具类之同步计数器(CountDownLatch)
- Java线程同步工具-CountDownLatch
- java 线程 CountDownLatch 同步工具
- 【5】Java并发编程:线程同步工具之CountDownLatch类
- 多线程编程入门(17):线程同步工具之CountDownLatch
- java多线程(13)--线程同步工具之CountDownLatch
- Android之CountDownLatch线程同步
- java线程同步之CountDownLatch
- 同步工具类之 CountDownLatch
- Windows环境下Python中wordcloud的使用——自己踩过的坑 2017.08.08
- 剑指offer--第一个只出现一次的字符
- windows列出所有进程示例
- idea 里maven项目自动加载jar包
- 设计模式---unity中的单例模式
- 线程同步工具之CountDownLatch示例
- 高性能MySQL——Count(1) OR Count(*)?
- Mybatis Dynamic Query 前言
- Java概述、Java语言基础、面向对象基础
- 后端框架
- C语言const
- mongo用户账号管理
- Poj1651 矩阵链乘
- js对象的可枚举性