package five;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.CountDownLatch;
public class CountDownLatches {
private static final int threadNumber = 10;
public static void main(String args[]) throws IOException,
InterruptedException {
test1();
}
public static void test1() throws IOException, InterruptedException {
final CountDownLatch startGate = new CountDownLatch(1);
final CountDownLatch endGate = new CountDownLatch(threadNumber);
for (int i = 0; i < threadNumber; i++) {
Thread t = new Thread() {
@Override
public void run() {
try {
startGate.await();// 判断锁存器倒计数据是否为0,否则等待
System.out.println(Thread.currentThread().getName()
+ " doSomething");
endGate.countDown();
} catch (InterruptedException e) {
}
}
};
t.start();
}
long start = System.nanoTime();
startGate.countDown(); // 将锁存器计数-1,如果为0,则因该latch阻塞的线程开始执行
endGate.await();
long end = System.nanoTime();
System.out.println(end-start);
}
}
CountDownLatch
最新推荐文章于 2024-03-17 21:20:55 发布