import java.util.concurrent.CountDownLatch;
public class TestThread extends Thread{
CountDownLatch cd;
String threadName;
public TestThread(CountDownLatch cd,String threadName){
this.cd=cd;
this.threadName=threadName;
}
@Override
public void run() {
System.out.println(threadName+" start working...");
dowork();
System.out.println(threadName+" end working and exit...");
cd.countDown();//告诉同步类完成一个线程操作完成
}
private void dowork(){
try {
Thread.sleep(2000);
System.out.println(threadName+" is working...");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.concurrent.CountDownLatch;
public class TsetCountDownLatch {
public static void main(String[] args) {
try {
CountDownLatch cd = new CountDownLatch(3);// 表示一共有三个线程
TestThread thread1 = new TestThread(cd, "thread1");
TestThread thread2 = new TestThread(cd, "thread2");
TestThread thread3 = new TestThread(cd, "thread3");
thread1.start();
thread2.start();
thread3.start();
cd.await();//等待所有线程完成
System.out.println("All Thread finishd");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
thread1 start working... thread2 start working... thread3 start working... thread2 is working... thread2 end working and exit... thread1 is working... thread3 is working... thread3 end working and exit... thread1 end working and exit... All Thread finishd
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有