线程同步工具之CountDownLatch示例

来源:互联网 发布:双色球破解软件 编辑:程序博客网 时间:2024/05/19 23:19

CountDownLatch的一个非常典型的应用场景是:有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行。假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减到0为止。

[java] view plain copy
print?
  1. package chapter3;  
  2.   
  3. import java.util.concurrent.CountDownLatch;  
  4.   
  5. public class Videoconference implements Runnable{  
  6.   
  7.     private final CountDownLatch controller;  
  8.     public Videoconference(int number){  
  9.         controller = new CountDownLatch(number);  
  10.     }  
  11.       
  12.     public void arrive(String name){  
  13.         System.out.println(name+” has arrived.”);  
  14.         controller.countDown();  
  15.         System.out.println(”VideoConference:Waiting for ”+controller.getCount());  
  16.     }  
  17.       
  18.     @Override  
  19.     public void run() {  
  20.   
  21.         System.out.println(”VideoConference:Initialization:”+controller.getCount());  
  22.           
  23.         try {  
  24.             controller.await();  
  25.             System.out.printf(”VideoConference: All the participants have come\n”);  
  26.             System.out.printf(”VideoConference: Let’s start…\n”);  
  27.   
  28.         } catch (InterruptedException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32. }  
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();        }    }}

[java] view plain copy
print?
  1. package chapter3;  
  2.   
  3. import java.util.concurrent.TimeUnit;  
  4.   
  5. public class Participant implements Runnable{  
  6.     private Videoconference conference;  
  7.     private String name;  
  8.       
  9.     public Participant(Videoconference conference,String name){  
  10.         this.conference = conference;  
  11.         this.name = name;  
  12.           
  13.           
  14.     }  
  15.       
  16.     @Override  
  17.     public void run() {  
  18.   
  19.         long duration = (long)(Math.random()*10);  
  20.         try {  
  21.             TimeUnit.SECONDS.sleep(duration);  
  22.         } catch (InterruptedException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         }  
  26.         conference.arrive(name);  
  27.     }  
  28.   
  29. }  
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);    }}

[java] view plain copy
print?
  1. package chapter3;  
  2. /** 
  3.  *  
  4.  * <p> 
  5.  * Description: CountDownLatch的学习 
  6.  * </p> 
  7.  * @author zhangjunshuai 
  8.  * @version 1.0 
  9.  * Create Date: 2014-9-25 下午8:11:55 
  10.  * Project Name: Java7Thread 
  11.  * 
  12.  * <pre> 
  13.  * Modification History:  
  14.   *             Date                                Author                   Version          Description  
  15.  * ———————————————————————————————————–   
  16.  * LastChange: Date::&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      Author:&nbsp;          Rev:&nbsp;          
  17.  * </pre> 
  18.  * 
  19.  */  
  20. public class Main2 {  
  21.   
  22.     /** 
  23.      * <p> 
  24.      * </p> 
  25.      * @author zhangjunshuai 
  26.      * @date 2014-9-25 下午8:11:50 
  27.      * @param args 
  28.      */  
  29.     public static void main(String[] args) {  
  30.         Videoconference conference = new Videoconference(9);  
  31.         Thread threadConference = new Thread(conference);  
  32.         threadConference.start();  
  33.         for(int i=0;i<10;i++){  
  34.             Participant p = new Participant(conference, “Participant”+i);  
  35.             Thread t = new Thread(p);  
  36.             t.start();  
  37.         }  
  38.     }  
  39.   
  40. }  
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个基本元素:

  1. 初始值决定CountDownLatch类需要等待的事件的数量。
  2. await() 方法, 被等待全部事件终结的线程调用。
  3. 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.