应用一(模拟压力测试,一个接口同时处理多个请求)
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class CountDownLatchTest {
private static final int RUNNER_COUNT = 20;
public static void main(String[] args) throws InterruptedException {
CountDownLatch start = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(RUNNER_COUNT);
for (int i = 0; i < RUNNER_COUNT; i++) {
new Thread("runner: " + i) {
@Override
public void run() {
long time = (long)(Math.random() * 5) + 2;
try {
System.out.println(this.getName() + " 开始等待。。。");
start.await();
TimeUnit.SECONDS.sleep(time);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(this.getName() + " 耗时" + time + "秒 执行完毕");
end.countDown();
}
}
}.start();
}
System.out.println("准备 Start ...");
TimeUnit.SECONDS.sleep(3);
System.out.println("全部放行。。。");
start.countDown();
end.await();
System.out.println("测试 End ...");
}
}
应用二 (模拟请求多个接口,最后一个接口参数依赖前面接口的响应)
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class WorkerThread extends Thread {
private long time;
private CountDownLatch countDownLatch;
public WorkerThread(String name, long time, CountDownLatch countDownLatch) {
super(name);
this.time = time;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
System.out.println(this.getName() + " 开始工作");
TimeUnit.SECONDS.sleep(this.time);
System.out.println(this.getName() + "工作完成, 耗时:"+ this.time + "秒");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
this.countDownLatch.countDown();
System.out.println("countDownLatch.getCount():" + countDownLatch.getCount());
}
}
}
import java.util.concurrent.CountDownLatch;
public class WorkerTheadTest {
private static final int COUNT = 5;
public static void main(String[] args) throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(COUNT);
for (int i = 0; i < COUNT; i++) {
WorkerThread worker = new WorkerThread("worker-" + i, (long)(Math.random() * 8) + 2, countDownLatch);
worker.start();
}
countDownLatch.await();
System.out.println("准备工作就绪");
WorkerThread workerLast = new WorkerThread("worker-last", (long)(Math.random() * 8) + 2, countDownLatch);
workerLast.start();
}
}
应用三 (模拟操作流程,前面的操作第一阶段完成,后面的操作才能开始)
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class WorkerThread2 extends Thread {
private long time;
private CountDownLatch countDownLatch;
public WorkerThread2(String name, long time, CountDownLatch countDownLatch) {
super(name);
this.time = time;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
System.out.println(this.getName() + " 开始阶段一");
TimeUnit.SECONDS.sleep(this.time);
System.out.println(this.getName() + "阶段一完成, 耗时:"+ this.time + "秒");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
this.countDownLatch.countDown();
System.out.println("countDownLatch.getCount():" + countDownLatch.getCount());
}
try {
System.out.println(this.getName() + " 开始阶段二");
TimeUnit.SECONDS.sleep(this.time);
System.out.println(this.getName() + "阶段二完成, 耗时:"+ this.time + "秒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.concurrent.CountDownLatch;
public class WorkerTheadTest2 {
private static final int COUNT = 5;
public static void main(String[] args) throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(COUNT);
for (int i = 0; i < COUNT; i++) {
WorkerThread2 worker = new WorkerThread2("worker-" + i, (long)(Math.random() * 8) + 2, countDownLatch);
worker.start();
}
countDownLatch.await();
System.out.println("准备工作就绪");
WorkerThread2 workerLast = new WorkerThread2("worker-last", (long)(Math.random() * 8) + 2, countDownLatch);
workerLast.start();
}
}