CountDownLatch 应用实例

参考:http://www.blogjava.net/freeman1984/archive/2011/07/04/353654.html

  在一些应用场合中,某段程序需要等待某个条件达到要求后才能执行,或者等待一定长的时间后此行,从jdk1.5开始就可以使用CountDownLatch实现,    
  CountDownLatch类是一个同步倒数计数器,构造时传入int参数,该参数就是计数器的初始值,每调用一次countDown()方法,计数器 减1,计数器大于0 时,await()方法会阻塞后面程序执行,直到计数器为0,await(long timeout, TimeUnit unit),是等待一定时间,然后执行,不管计数器是否到0了。
 下面举一个等车的例子:
 10个同学上车,车等待同学上车,如果有等待时间限制,到时间就开走,不管学生上没上完。如果没有等待时间,学生上完了再开:

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Student {
	private int myNum;
	public Student(int num) {
		this.myNum = num;
	}
	
	public boolean getOn() throws InterruptedException {
		//Thread.currentThread().sleep(new Random().nextInt(2) * 1000);// 小朋友上车是需要时间的
		//TimeUnit.MICROSECONDS.sleep(new Random().nextInt(2) * 1000);// JDK1.5 后使用这个方式
		TimeUnit.SECONDS.sleep(2);
		if (CountDownLatchTest.isGone) {
			return false;
		}
		System.out.print("编号为:"+myNum+"的同学上车了..");
		return true;
	}
}

 

import java.util.concurrent.CountDownLatch;

public class GetOn implements Runnable {

	private CountDownLatch		countDownLatch;
	
	public GetOn(CountDownLatch		countDownLatch) {
		this.countDownLatch = countDownLatch;
	}
	
	@Override
	public void run() {
		for (int i = 0; i < CountDownLatchTest.numberOfPeople; i++) {
			try {
				if (CountDownLatchTest.isGone) {
					 System.out.println("还差:"+countDownLatch.getCount()+" 个没娃上车呢.怎么车走了");
					 break;
				} 
				boolean goonSuccess = new Student(i + 1).getOn();//顺序上车
				if (goonSuccess) {
					// 每当小朋友上车成功后,就调用 countDown() 进行减1操作
					countDownLatch.countDown();
				}
			} catch (InterruptedException  e) {}
			if (countDownLatch.getCount() != 0) {
				System.out.println("还差:"+(countDownLatch.getCount())+" 个没上车");
			} else {
				System.out.println("都上车了");
			}
		}
	}
}

 

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchTest {

	public static int numberOfPeople = 10;//等车的学生数
	
	public static boolean isGone = false;//车开的标志
	
	public static int carWaitTime = 8;//车等的时间
	
	public static void main(String[] args) throws InterruptedException {
		
		CountDownLatch countDownLatch = new CountDownLatch(numberOfPeople);
		new Thread(new GetOn(countDownLatch)).start();
		waitStudentGetOn(countDownLatch); //等所有的学生上车
		driveHome();
	}
	
	public static void waitStudentGetOn(CountDownLatch waitStudentsGetOn) throws InterruptedException {
		 System.out.println("赶紧的,抓紧时间上车..");
		 //waitStudentsGetOn.await(carWaitTime, TimeUnit.SECONDS);//等5秒,还没上车,就开走。。
		 waitStudentsGetOn.await();// 等待所有人都上车,才走开
	}
	
	public static void driveHome() throws InterruptedException {
		System.out.println("开车,鞋儿破 帽儿破 身上的袈裟破 你笑我 他笑我 一把扇儿破");
		isGone = true;
	}
}

 

你可能感兴趣的:(CountDownLatch)