package com.ane56.dispatch.application;
import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
/**
* @author 冒云龙
* @date 2017年4月10日 上午9:13:06
* @describe
*/
public class CountDownLatchTest {
final static SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
public static void main(String[] args){
CountDownLatch latch = new CountDownLatch(2);
Worker worker = new Worker("Tom", 1000, latch);
Worker worker2 = new Worker("Sam", 2000, latch);
worker.start();
worker2.start();
}
static class Worker extends Thread{
private String name;
private int workTime;
private CountDownLatch latch;
public Worker(String name,int workTime,CountDownLatch latch){
this.name=name;
this.workTime=workTime;
this.latch=latch;
}
public void run(){
System.out.println(name+"开始工作"+SIMPLE_DATE_FORMAT.format(new java.util.Date()));
doWork();
System.out.println(name+"工作结束"+SIMPLE_DATE_FORMAT.format(new java.util.Date()));
latch.countDown();
System.out.println(latch);
}
private void doWork() {
try {
Thread.sleep(workTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
CountDownLaunch
最新推荐文章于 2023-12-25 21:49:44 发布