[quote="AliKevin"]CountDownLatch位于java.util.concurrent包中,它是一个通用同步工具,它有很多用途。我们利用将CountDownLatch要进行初始化,用1初始化的 CountDownLatch 用作一个简单的开/关锁存器或入口然后在通过调用 countDown() 的线程打开入口前,所有调用 await 的线程都一直在入口处等待。用 N 初始化的 CountDownLatch 可以使一个线程在 N 个线程完成某项操作之前一直等待,或者使其在某项操作完成 N 次之前一直等待。[/quote]
[b]用法示例:[/b] 我们假定一个短跑比赛的场景,裁判员会有两个信号:
第一个信号在运动员做好准备的时候发送给运动员一个起跑的信号,等待裁判起跑信号的运动员会一起起跑。
第二个信号是裁判员要根据所有参赛运动员给出跑完的信号后宣布比赛结束。
[b]1.Referee.java[/b]
[b]2.Player.java[/b]
[b]3.Match.java[/b]
[b]4.运行显示结果[/b]
[b]用法示例:[/b] 我们假定一个短跑比赛的场景,裁判员会有两个信号:
第一个信号在运动员做好准备的时候发送给运动员一个起跑的信号,等待裁判起跑信号的运动员会一起起跑。
第二个信号是裁判员要根据所有参赛运动员给出跑完的信号后宣布比赛结束。
[b]1.Referee.java[/b]
package study.java.util.concurrent.CountDownLatch.demo2;
import java.util.concurrent.CountDownLatch;
/**
* TODO Comment of Referee
*
* @author AliKevin
*/
public class Referee implements Runnable {
private CountDownLatch startSignal;
private CountDownLatch finishSignal;
/**
* @param startSignal
* @param finishSignal
*/
public Referee(CountDownLatch startSignal, CountDownLatch finishSignal) {
this.startSignal = startSignal;
this.finishSignal = finishSignal;
}
/*
* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
System.out.println("Referee say:start run ...");
this.startSignal.countDown();
try {
this.finishSignal.await();
System.out.println("Referee say:match finish...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
[b]2.Player.java[/b]
package study.java.util.concurrent.CountDownLatch.demo2;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* TODO Comment of Player
*
* @author AliKevin
*/
public class Player implements Runnable {
private int playerNum;
private CountDownLatch startSignal;
private CountDownLatch finishSignal;
/**
* @param startSignal
* @param finishSignal
*/
public Player(CountDownLatch startSignal, CountDownLatch finishSignal, int playerNum) {
this.startSignal = startSignal;
this.finishSignal = finishSignal;
this.playerNum = playerNum;
}
/*
* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
System.out.println("player " + this.playerNum + " is ready to run ...");
startSignal.await();
System.out.println("player " + this.playerNum + " is begin to run ...");
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
System.out.println("player " + this.playerNum + " is finish ...");
finishSignal.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
[b]3.Match.java[/b]
package study.java.util.concurrent.CountDownLatch.demo2;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* TODO Comment of Match
*
* @author AliKevin
*/
public class Match {
/**
* @param args
*/
public static void main(String[] args) {
try {
ExecutorService executor = Executors.newCachedThreadPool();
int start = 1;
int finish = 10;
CountDownLatch startSignal = new CountDownLatch(start);
CountDownLatch finishSignal = new CountDownLatch(finish);
Referee referee = new Referee(startSignal, finishSignal);
for (int i = 1; i <= finish; i++) {
executor.execute(new Player(startSignal, finishSignal, i));
Thread.sleep(1000);
}
executor.execute(referee);
Thread.sleep(10000);
executor.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
[b]4.运行显示结果[/b]
player 1 is ready to run ...
player 2 is ready to run ...
player 3 is ready to run ...
player 4 is ready to run ...
player 5 is ready to run ...
player 6 is ready to run ...
player 7 is ready to run ...
player 8 is ready to run ...
player 9 is ready to run ...
player 10 is ready to run ...
Referee say:start run ...
player 1 is begin to run ...
player 3 is begin to run ...
player 6 is begin to run ...
player 7 is begin to run ...
player 9 is begin to run ...
player 2 is begin to run ...
player 10 is begin to run ...
player 8 is begin to run ...
player 5 is begin to run ...
player 2 is finish ...
player 4 is begin to run ...
player 8 is finish ...
player 7 is finish ...
player 5 is finish ...
player 10 is finish ...
player 1 is finish ...
player 6 is finish ...
player 4 is finish ...
player 9 is finish ...
player 3 is finish ...
Referee say:match finish...