/**主线程挂起,作为守护线程。线程池里,每当一个线程结束后,计数器减一。当线程池里所有线程都结束后,通知主线程,使得主线程可以继续。
* @author liujie
* @version 1.0
* @since 2017年2月15日 下午5:48:24
*/
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestCountDownLatch1 {
public static void main(String[] args) {
final int num = 1000;
// 线程通信计数器
CountDownLatch end = new CountDownLatch(num);
// 初始化任务线程池
ExecutorService exe = Executors.newFixedThreadPool(num);
for (int n = 0; n < num; ++n) {
exe.execute(new MyRunable(end, n));
}
try {
end.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class MyRunable implements Runnable {
private CountDownLatch end;
private int id;
public MyRunable(CountDownLatch end, int id) {
this.end = end;
this.id = id;
}
public void run() {
System.out.println();
System.out.println(String.format("Begin:id=%s;end=%s", id, end.getCount()));
end.countDown();
System.out.println(String.format("End:id=%s;end=%s", id, end.getCount()));
System.out.println();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
package com.oec.utils;
/**
* @author liujie
* @version 1.0
* @since 2017年2月15日 下午3:49:22
*/
import java.util.concurrent.CountDownLatch;
/**
* 假设我们要打印1-100,最后再输出“Ok“。1-100的打印顺序不要求统一,只需保证“Ok“是在最后出现即可。
* 示例:CountDownLatch的使用举例 Mail: ken@iamcoding.com
*
* @author janeky
*/
public class TestCountDownLatch {
private static final int N = 10;
public static void main(String[] args) throws InterruptedException {
//等待10 倒计时
CountDownLatch doneSignal = new CountDownLatch(N);
CountDownLatch startSignal = new CountDownLatch(1);// 开始执行信号
for (int i = 1; i <= N; i++) {
new Thread(new Worker(i, doneSignal, startSignal)).start();// 线程启动了
}
System.out.println("begin------------");
startSignal.countDown();// 开始执行啦
doneSignal.await();// 等待所有的线程执行完毕
System.out.println("Ok");
}
static class Worker implements Runnable {
private final CountDownLatch doneSignal;
private final CountDownLatch startSignal;
private int beginIndex;
Worker(int beginIndex, CountDownLatch doneSignal, CountDownLatch startSignal) {
this.startSignal = startSignal;
this.beginIndex = beginIndex;
this.doneSignal = doneSignal;
}
public void run() {
try {
startSignal.await(); // 等待开始执行信号的发布
beginIndex = (beginIndex - 1) * 10 + 1;
for (int i = beginIndex; i <= beginIndex + 10; i++) {
System.out.println(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
doneSignal.countDown();
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------
package com.oec.utils;
/**
* @author liujie
* @version 1.0
* @since 2017年2月15日 下午5:41:59
*/
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 多个运动员等待裁判命令:裁判等所有运动员到齐后发布结果
*
* @author liujie
* @version 1.0
* @since 2017年2月15日 下午5:42:19
*/
public class CountdownLatchTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
// 裁判发布命令的计数器,计数器为0,运动员就跑
final CountDownLatch cdOrder = new CountDownLatch(1);
// 运动员跑到终点的计数器,为0裁判宣布结果
final CountDownLatch cdAnswer = new CountDownLatch(3);
// 产生3个运动员
for (int i = 0; i < 3; i++) {
Runnable runnable = new Runnable() {
public void run() {
try {
System.out.println("线程" + Thread.currentThread().getName() + "正准备接受命令");
cdOrder.await();
System.out.println("线程" + Thread.currentThread().getName() + "已接受命令");
Thread.sleep((long) (Math.random() * 10000));
System.out.println("线程" + Thread.currentThread().getName() + "回应命令处理结果");
cdAnswer.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
};
service.execute(runnable); // 运动员开始任务
}
try {
// 裁判休息一会 再发布命令
Thread.sleep((long) (Math.random() * 10000));
System.out.println("线程" + Thread.currentThread().getName() + "即将发布命令");
cdOrder.countDown(); // 命令计数器置为0,发布命令
System.out.println("线程" + Thread.currentThread().getName() + "已发送命令,正在等待结果");
cdAnswer.await(); // 等待所有运动员,计数器为0 所有运动员到位
System.out.println("线程" + Thread.currentThread().getName() + "已收到所有响应结果");
} catch (Exception e) {
e.printStackTrace();
}
service.shutdown();
}
}