CountDownLatch的用法
来源:互联网 发布:org.apache.commons包 编辑:程序博客网 时间:2024/06/13 13:57
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
主要方法
public CountDownLatch(int count);
public void countDown();
主要方法
public CountDownLatch(int count);
public void countDown();
public void await()
举例:
import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.CountDownLatch;public class CountDownLatchTest {private 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("bao", 2000, latch); Worker worker2=new Worker("senan", 4000, latch); worker1.start(); worker2.start(); latch.await();//等待所有worker完成工作 System.out.println("all work done at "+sdf.format(new Date())); }static class Worker extends Thread{private String workerName;private int workTime;private 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(workerName + " start at" + sdf.format(new Date()));try {Thread.sleep(workTime);} catch (InterruptedException e) {e.printStackTrace();}finally{latch.countDown();//计数器减一}System.out.println(workerName + " end at" + sdf.format(new Date()));}}}
0 0
- countdownlatch的用法
- CountDownLatch的用法
- CountDownLatch的用法
- 闭锁CountDownLatch的用法
- CountDownLatch的用法
- 浅谈CountDownLatch的用法
- CountDownLatch的用法
- CountDownLatch和CyclicBarrier 的用法
- Java中CountDownLatch的用法
- Java中CountDownLatch的用法
- CountDownLatch用法
- CountDownLatch用法
- CountDownLatch用法
- CountDownLatch用法
- CountDownLatch 用法
- CountDownLatch用法
- CountDownLatch用法
- CountDownLatch用法
- http协议分块传输编码
- 读写CSV文件
- Wooden Sticks
- ubuntu12 配置samba服务 实现文件共享
- iOS笔记-(缓存机制的理解与实现)
- CountDownLatch的用法
- android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
- Lunch Time【The 12th Zhejiang Provincial Collegiate Programming Contest】
- LeetCode Recover Binary Search Tree
- 杭电 HDU ACM 1171 Big Event in HDU
- 《C++ primer》习题4.33
- 12th浙江省省赛 J Convert QWERTY to Dvorak(细节模拟题)
- CMake Error:Failed to find "glu32" in "" with CMAKE_CXX_LIBRARY_ARCHITECTURE "".
- 调侃《Head First 设计模式》之命令模式