CountDownLatch应用

应用一(模拟压力测试,一个接口同时处理多个请求)

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();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值