利用CountDownLatch实现的一个简单的线程同步场景

package countdownlatchTest;

import java.util.concurrent.CountDownLatch;

class Worker {    
	private String name;        // 名字    
	private long workDuration;  // 工作持续时间      
	public Worker(String name, long workDuration) {        
		this.name = name;        
		this.workDuration = workDuration;    
		System.out.println("Worker: " + name + " is assigned with work time: " + workDuration);
	}      
	
	public void doWork() {        
		System.out.println(name + " begins to work...");        
		try {            
			Thread.sleep(workDuration); // 用休眠模拟工作执行的时间        
		} catch(InterruptedException ex) {
			ex.printStackTrace();        
		}
		System.out.println(name + " has finished the job...");    
	} 
}

class WorkerTestThread implements Runnable {    
	private Worker worker;    
	private CountDownLatch cdLatch;      
	public WorkerTestThread(Worker worker, CountDownLatch cdLatch) {        
		this.worker = worker;        
		this.cdLatch = cdLatch;    
	}      
	
	@Override    
	public void run() {        
		worker.doWork();        // 让工人开始工作        
		cdLatch.countDown();    // 工作完成后倒计时次数减1    
	} 
	
}

public class CountDownLatchTest {
	private static final int MAX_WORK_DURATION = 5000;  // 最大工作时间    
	private static final int MIN_WORK_DURATION = 1000;  // 最小工作时间      
	// 产生随机的工作时间    
	private static long getRandomWorkDuration(long min, long max) {        
		return (long) (Math.random() * (max - min) + min);    
	}      
	
	public static void main(String[] args) {    
		/* 2是次数,不是时间数
		 * 将这个CountDownLatch引用赋给工作线程,每次工作线程完成任务后,调用
		 * CountDownLatch.countDown, 将计数器减一。如果技术器减到0,阻塞的await方法
		 * 才会返回,重新获得控制权
		 */
		CountDownLatch latch = new CountDownLatch(2);  // should be exactly 2
		Worker w1 = new Worker("Jerry Worker 1", getRandomWorkDuration(MIN_WORK_DURATION, MAX_WORK_DURATION));  
		Worker w2 = new Worker("Jerry Worker 2", getRandomWorkDuration(MIN_WORK_DURATION, MAX_WORK_DURATION));  
		new Thread(new WorkerTestThread(w1, latch)).start();        
		new Thread(new WorkerTestThread(w2, latch)).start();          
		// latch.countDown();
		try {          
			// 仅当CountDownLatch的count降低到0时,这个阻塞的方法才会返回
			latch.await(); 
			System.out.println("All jobs have been finished!");        
		} 
		catch (InterruptedException e) {            
			e.printStackTrace();
		}
	}
}

展开阅读全文
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值