标签:style http io color os ar java for sp
主要方法
public CountDownLatch(int count);
public void countDown();
public void await() throws InterruptedException
构造方法参数指定了计数的次数
countDown方法,当前线程调用此方法,则计数减一
awaint方法,调用此方法会一直阻塞当前线程,直到计时器的值为0
例子
Java代码 <EMBED type=application/x-shockwave-flash pluginspage=http://www.macromedia.com/go/getflashplayer height=15 width=14 src=http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf allowscriptaccess="always" quality="high" flashvars="clipboard=public%20class%20CountDownLatchDemo%20%7B%0A%09final%20static%20SimpleDateFormat%20sdf%3Dnew%20SimpleDateFormat(%22yyyy-MM-dd%20HH%3Amm%3Ass%22)%3B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20throws%20InterruptedException%20%7B%0A%20%20%20%20%09CountDownLatch%20latch%3Dnew%20CountDownLatch(2)%3B%2F%2F%E4%B8%A4%E4%B8%AA%E5%B7%A5%E4%BA%BA%E7%9A%84%E5%8D%8F%E4%BD%9C%0A%20%20%20%20%09Worker%20worker1%3Dnew%20Worker(%22zhang%20san%22%2C%205000%2C%20latch)%3B%0A%20%20%20%20%09Worker%20worker2%3Dnew%20Worker(%22li%20si%22%2C%208000%2C%20latch)%3B%0A%20%20%20%20%09worker1.start()%3B%2F%2F%0A%20%20%20%20%09worker2.start()%3B%2F%2F%0A%20%20%20%20%09latch.await()%3B%2F%2F%E7%AD%89%E5%BE%85%E6%89%80%E6%9C%89%E5%B7%A5%E4%BA%BA%E5%AE%8C%E6%88%90%E5%B7%A5%E4%BD%9C%0A%20%20%20%20%20%20%20%20System.out.println(%22all%20work%20done%20at%20%22%2Bsdf.format(new%20Date()))%3B%0A%09%7D%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20static%20class%20Worker%20extends%20Thread%7B%0A%20%20%20%20%09String%20workerName%3B%20%0A%20%20%20%20%09int%20workTime%3B%0A%20%20%20%20%09CountDownLatch%20latch%3B%0A%20%20%20%20%09public%20Worker(String%20workerName%20%2Cint%20workTime%20%2CCountDownLatch%20latch)%7B%0A%20%20%20%20%09%09%20this.workerName%3DworkerName%3B%0A%20%20%20%20%09%09%20this.workTime%3DworkTime%3B%0A%20%20%20%20%09%09%20this.latch%3Dlatch%3B%0A%20%20%20%20%09%7D%0A%20%20%20%20%09public%20void%20run()%7B%0A%20%20%20%20%09%09System.out.println(%22Worker%20%22%2BworkerName%2B%22%20do%20work%20begin%20at%20%22%2Bsdf.format(new%20Date()))%3B%0A%20%20%20%20%09%09doWork()%3B%2F%2F%E5%B7%A5%E4%BD%9C%E4%BA%86%0A%20%20%20%20%09%09System.out.println(%22Worker%20%22%2BworkerName%2B%22%20do%20work%20complete%20at%20%22%2Bsdf.format(new%20Date()))%3B%0A%20%20%20%20%09%09latch.countDown()%3B%2F%2F%E5%B7%A5%E4%BA%BA%E5%AE%8C%E6%88%90%E5%B7%A5%E4%BD%9C%EF%BC%8C%E8%AE%A1%E6%95%B0%E5%99%A8%E5%87%8F%E4%B8%80%0A%0A%20%20%20%20%09%7D%0A%20%20%20%20%09%0A%20%20%20%20%09private%20void%20doWork()%7B%0A%20%20%20%20%09%09try%20%7B%0A%09%09%09%09Thread.sleep(workTime)%3B%0A%09%09%09%7D%20catch%20(InterruptedException%20e)%20%7B%0A%09%09%09%09e.printStackTrace()%3B%0A%09%09%09%7D%0A%20%20%20%20%09%7D%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%20%0A%7D%0A" wmode="transparent">
public class CountDownLatchDemo {
final static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch=new CountDownLatch(2);//两个工人的协作
Worker worker1=new Worker("zhang san", 5000, latch);
Worker worker2=new Worker("li si", 8000, latch);
worker1.start();//
worker2.start();//
latch.await();//等待所有工人完成工作
System.out.println("all work done at "+sdf.format(new Date()));
}
static class Worker extends Thread{
String workerName;
int workTime;
CountDownLatch latch;
public Worker(String workerName ,int workTime ,CountDownLatch latch){
this.workerName=workerName;
this.workTime=workTime;
this.latch=latch;
}
public void run(){
System.out.println("Worker "+workerName+" do work begin at "+sdf.format(new Date()));
doWork();//工作了
System.out.println("Worker "+workerName+" do work complete at "+sdf.format(new Date()));
latch.countDown();//工人完成工作,计数器减一
}
private void doWork(){
try {
Thread.sleep(workTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出:
Worker zhang san do work begin at 2011-04-14 11:05:11
Worker li si do work begin at 2011-04-14 11:05:11
Worker zhang san do work complete at 2011-04-14 11:05:16
Worker li si do work complete at 2011-04-14 11:05:19
all work done at 2011-04-14 11:05:19
标签:style http io color os ar java for sp
原文地址:http://my.oschina.net/tinglanrmb32/blog/339661