您的位置:首页 > 其它

线程同步-CountDownLatch

2017-02-13 21:03 211 查看
应用场景:

有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行。

假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减

到0为止。

 

主要方法:

 public CountDownLatch(int count);

 public void countDown();

 public void await() throws InterruptedException                             
此方法休眠中断条件: CountDownLatch 内部计数器到达0或者特定的时间过去了。

public void await(long timeout, TimeUnit unit) throws InterruptedException                  此方法休眠中断条件: CountDownLatch 内部计数器到达0或者特定的时间过去了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: