java多线程 等待多个并发事件的完成【转】

来自:http://blog.csdn.net/qq_27603235/article/details/50651697

java API中提供了CountDownLatch类,它允许线程一直等待,知道等待操作结束。下面以一个视频会议等待人数为例。 
1.视频会议类,实现Runnable接口。

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.printf("%s has arrived.",name);
        controller.countDown();
        //还没有到达的人数
        System.out.printf("Videoconferencr has waitinf for %d participants\n",controller.getCount());
    }
    public void run() { 
    System.out.printf("Videoconference initialization:%d participants.\n",controller.getCount());
    try {
        //等待所有人到达,如果没有这句,则会在参与人没到达之前开始会议
        controller.await();
        System.out.printf("Videoconference:ALL participants has come\n");
        System.out.print("Videoconference:Let is start...\n");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2.与会者类


import java.util.concurrent.TimeUnit;

public class Participant implements Runnable{
private Videoconference conference;
public 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) {
        e.printStackTrace();
    }
    conference.arrive(name);
}

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3.主方法

package Thread03semaphone;

public class Main2 {
public static void main(String[] args){
    Videoconference videoConference = new Videoconference(10);
    Thread threadconference = new Thread(videoConference);
    threadconference.start();
    for(int i =0;i<10;i++){
        Participant p = new Participant(videoConference,"Participant"+i);
        Thread t = new Thread(p);
        t.start();
    }
}
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

当创建CountDownLatch对象时,通过构造器来初始化内部计数器(本例中为10),当countdown()方法被调用时,计数器将减1,当计数器为0时,countdownlatch将唤醒所有等待的线程。

注意点:

  1. CountDownLatch对象计数器一旦被初始化之后,就不可以修改。
  2. 他并不是用来保护临界资源或者同步代码块,只是用来同步多个任务的多个线程。
  3. CountDownLatch对象只能被使用一次,一旦计数器为0时,如果再需要使用,则需要重新New一个CountDownLatch对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值