(三)線程同步工具集_3---等待多個併發事件

等待多個併發事件

Java concurrent API 提供了一個類,能夠使一個或多個線程去等待一系列操做完成,該類是 CountDownLatch,該類初始化一個整數,這個整數表明了線程要等待的操做個數,
當一個線程等待操做完成時調用await()方法,當一個操做結束後,調用countDown()方法;
在下面的例子中展現CounDownLatch的使用,這是一個很是有意思的例子,一個視頻會議,多個參與者;一個線程等待參與者的到來,直到全部的參與者都到達後,該線程才繼續執行;

動手實現

1.建立VideoConference
public class VideoConference implements Runnable {
    private final CountDownLatch controller;

    public VideoConference(int number) {
        this.controller = new CountDownLatch(number);
    }
    public void arrive(String name) {
        System.out.printf("%s has arrived.\n", name);
        controller.countDown();
        System.out.printf("VideoConference: Waiting for %d participants.\n",
                controller.getCount());
    }

    @Override
    public void run() {
        System.out.printf("VideoConference: Initialization: %d participants.\n",
                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();
        }
    }
}
2.建立Participant
public class Participant implements Runnable {
    private VideoConference videoConference;
    private String name;

    public Participant(VideoConference videoConference, String name) {
        this.videoConference = videoConference;
        this.name = name;
    }

    @Override
    public void run() {
        long duration=(long)(Math.random()*10);
        try {
            TimeUnit.SECONDS.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        videoConference.arrive(name);
    }
}
3.Main
public class Main {
    public static void main(String[] args) {
        VideoConference conference = new VideoConference(10);
        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();
        }
    }
}
一次運行結果:
VideoConference: Initialization: 10 participants.
Participant 4 has arrived.
VideoConference: Waiting for 9 participants.
Participant 7 has arrived.
Participant 6 has arrived.
VideoConference: Waiting for 7 participants.
VideoConference: Waiting for 8 participants.
Participant 8 has arrived.
VideoConference: Waiting for 6 participants.
Participant 2 has arrived.
VideoConference: Waiting for 5 participants.
Participant 3 has arrived.
VideoConference: Waiting for 4 participants.
Participant 9 has arrived.
VideoConference: Waiting for 3 participants.
Participant 0 has arrived.
Participant 1 has arrived.
VideoConference: Waiting for 1 participants.
Participant 5 has arrived.
VideoConference: Waiting for 2 participants.
VideoConference: All the participants have come
VideoConference: Waiting for 0 participants.
VideoConference: Let's start...

要點

1.初始化操做序列個數
2.調用await()方法
3.調用countDown()方法
須要注意:當CounDownLatch中的內部計數爲0時,將會喚醒全部被await()方法睡眠的線程;該內部計數器只能被初始化一次;當countDown()方法達到0時,在調用將不會其任何做用;
相關文章
相關標籤/搜索