线程同步工具(三)等待多个并发事件完成

    xiaoxiao2023-12-15  171

    声明:本文是《 Java 7 Concurrency Cookbook 》的第三章, 作者: Javier Fernández González 译者:郑玉婷

    等待多个并发事件完成

    Java并发API提供这样的类,它允许1个或者多个线程一直等待,直到一组操作执行完成。 这个类就是CountDownLatch类。它初始一个整数值,此值是线程将要等待的操作数。当某个线程为了想要执行这些操作而等待时, 它要使用 await()方法。此方法让线程进入休眠直到操作完成。 当某个操作结束,它使用countDown() 方法来减少CountDownLatch类的内部计数器。当计数器到达0时,这个类会唤醒全部使用await() 方法休眠的线程们。

    在这个指南,你将学习如果使用 CountDownLatch 类来实现 video- conference 系统。 video-conference 系统将等待全部参与者到达后才会开始。

    准备

    指南中的例子是使用Eclipse IDE 来实现的。如果你使用Eclipse 或者其他的IDE,例如NetBeans,打开并创建一个新的java任务。

    怎么做呢

    按照这些步骤来实现下面的例子::

    01//1.   创建一个类名为 Videoconference 并特别实现 Runnable 接口。这个类将实现 video-conference 系统。 02 public class Videoconference implements Runnable{ 03  04//2.   声明 CountDownLatch 对象名为 controller。 05 private final CountDownLatch controller; 06  07//3.   实现类的构造函数,初始 CountDownLatch 属性。Videoconference 类接收将要等待的参与者的量为参数。 08 public Videoconference(int number) { 09     controller=new CountDownLatch(number); 10} 11  12//4.   实现 arrive() 方法。每次有参与者到达都会调用此方法。它接收String类型的参数名为 name。 13 public void arrive(String name){ 14  15//5.   首先,它输出某某参数已经到达。 16System.out.printf("%s has arrived.",name); 17  18//6.   然后,调用CountDownLatch对象的 countDown() 方法。 19controller.countDown(); 20  21//7.    最后,使用CountDownLatch对象的 getCount() 方法输出另一条关于还未确定到达的参与者数。 22 System.out.printf("VideoConference: Waiting for %d participants.\n",controller.getCount()); 23  24//8.   实现video-conference 系统的主方法。它是每个Runnable都必须有的 run() 方法。 25@Override 26 public void run() { 27  28//9.   首先,使用 getCount() 方法来输出这次video conference的参与值的数量信息。 29System.out.printf("VideoConference: Initialization: %d participants.\n",controller.getCount()); 30  31//10. 然后, 使用 await() 方法来等待全部的参与者。由于此法会抛出 InterruptedException 异常,所以要包含处理代码。 32 try { 33controller.await(); 34  35//11. 最后,输出信息表明全部参与者已经到达。 36System.out.printf("VideoConference: All the participants have come\n"); 37System.out.printf("VideoConference: Let's start...\n"); 38 } catch (InterruptedException e) { 39     e.printStackTrace(); 40} 41  42//12. 创建 Participant 类并实现 Runnable 接口。这个类表示每个video conference的参与者。 43 public class Participant implements Runnable { 44  45//13. 声明一个私有 Videoconference 属性名为 conference. 46 private Videoconference conference; 47  48//14. 声明一个私有 String 属性名为 name。 49 private String name; 50  51//15. 实现类的构造函数,初始化那2个属性。 52 public Participant(Videoconference conference, String name) { 53     this.conference=conference; 54     this.name=name; 55} 56  57//16. 实现参与者的run() 方法。 58@Override 59 public void run() { 60  61//17.  首先,让线程随机休眠一段时间。 62 long duration=(long)(Math.random()*10); 63 try { 64     TimeUnit.SECONDS.sleep(duration); 65 } catch (InterruptedException e) { 66     e.printStackTrace(); 67} 68  69//18. 然后,使用Videoconference 对象的arrive() 方法来表明参与者的到达。 70conference.arrive(name); 71  72//19. 最后,实现例子的 main 类通过创建一个名为 Main 的类并为其添加 main() 方法。 73 public class Main { 74  75 public static void main(String[] args) { 76  77//20. 创建 Videoconference 对象名为 conference,将等待10个参与者。 78 Videoconference conference=new Videoconference(10); 79  80//21. 创建 Thread 来运行这个 Videoconference 对象并开始运行。 81 Thread threadConference=new Thread(conference); 82threadConference.start(); 83  84//22. 创建 10个 Participant 对象,为每个对象各创建一个 Thread 对象来运行他们,开始运行全部的线程。 85 for (int i=0; i<10; i++){ 86     Participant p=new Participant(conference, "Participant "+i); 87     Thread t=new Thread(p); 88     t.start(); 89}

    它是怎么工作的…

    CountDownLatch类有3个基本元素:

    初始值决定CountDownLatch类需要等待的事件的数量。await() 方法, 被等待全部事件终结的线程调用。countDown() 方法,事件在结束执行后调用。

    当创建 CountDownLatch 对象时,对象使用构造函数的参数来初始化内部计数器。每次调用 countDown() 方法, CountDownLatch 对象内部计数器减一。当内部计数器达到0时, CountDownLatch 对象唤醒全部使用 await() 方法睡眠的线程们。

    不可能重新初始化或者修改CountDownLatch对象的内部计数器的值。一旦计数器的值初始后,唯一可以修改它的方法就是之前用的 countDown() 方法。当计数器到达0时, 全部调用 await() 方法会立刻返回,接下来任何countDown() 方法的调用都将不会造成任何影响。

    此方法与其他同步方法有这些不同:

    CountDownLatch 机制不是用来保护共享资源或者临界区。它是用来同步一个或者多个执行多个任务的线程。它只能使用一次。像之前解说的,一旦CountDownLatch的计数器到达0,任何对它的方法的调用都是无效的。如果你想再次同步,你必须创建新的对象。

    以下截图是例子的执行输出:

    你可以发现最后的参与者到达后,内部计数器一到达0,CountDownLatch对象就叫醒全部的 Videoconference 对象,他们全部输出信息表示video conference可以开始了。

    更多…

    CountDownLatch 类有另一种版本的 await() 方法,它是:

    await(long time, TimeUnit unit): 此方法会休眠直到被中断; CountDownLatch 内部计数器到达0或者特定的时间过去了。TimeUnit 类包含了:DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, 和 SECONDS.文章转自 并发编程网-ifeve.com
    最新回复(0)