java主线程控制子线程-尊龙游戏旗舰厅官网
1.[代码]countdownlatch控制主线程等子线程执行完--java多线程
package com.sihuatech.common;
import java.util.concurrent.countdownlatch;
/**
* 执行线程
*
* @author lei 2011-7-28
*/
class importthread extends thread {
private countdownlatch threadssignal;
public importthread(countdownlatch threadssignal) {
this.threadssignal = threadssignal;
}
@override
public void run() {
system.out.println(thread.currentthread().getname() "开始...");
// do somethings
threadssignal.countdown();// 线程结束时计数器减1
system.out.println(thread.currentthread().getname() "结束. 还有" threadssignal.getcount() " 个线程");
}
}
/**
* 测试方法
*
* @author lei 2011-7-28
*/
public class test {
public static void main(string[] args) {
int threadnum = 10;
countdownlatch threadsignal = new countdownlatch(threadnum);// 初始化countdown
for (int ii = 0; ii < threadnum; ii ) {// 开threadnum个线程
thread t = new importthread(threadsignal);
t.start();
}
try {
threadsignal.await();// 等待所有子线程执行完
} catch (interruptedexception e) {
e.printstacktrace();
}
system.out.println(thread.currentthread().getname() "结束.");
}
}
总结
以上是尊龙游戏旗舰厅官网为你收集整理的java主线程控制子线程_countdownlatch控制主线程等子线程执行完--java多线程的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: