Java如何等待子线程执行结束
1个回答
展开全部
先调用
shutdown
在调用
isTerminated
例:
/*
* 采用线程池开启多个子线程,主线程等待所有的子线程执行完毕
*/
public static void moreThread() {
try {
int threadNum = 0;
for (int i = 0; i < 10; i++) {
threadNum++;
final int currentThreadNum = threadNum;
exe.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("子线程[" + currentThreadNum + "]开启");
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("子线程[" + currentThreadNum + "]结束");
}
}
});
}
System.out.println("已经开启所有的子线程");
exe.shutdown();
System.out.println("shutdown():启动一次顺序关闭,执行以前提交的任务,但不接受新任务。");
while(true){
if(exe.isTerminated()){
System.out.println("所有的子线程都结束了!");
break;
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("主线程结束");
}
}
shutdown
在调用
isTerminated
例:
/*
* 采用线程池开启多个子线程,主线程等待所有的子线程执行完毕
*/
public static void moreThread() {
try {
int threadNum = 0;
for (int i = 0; i < 10; i++) {
threadNum++;
final int currentThreadNum = threadNum;
exe.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println("子线程[" + currentThreadNum + "]开启");
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("子线程[" + currentThreadNum + "]结束");
}
}
});
}
System.out.println("已经开启所有的子线程");
exe.shutdown();
System.out.println("shutdown():启动一次顺序关闭,执行以前提交的任务,但不接受新任务。");
while(true){
if(exe.isTerminated()){
System.out.println("所有的子线程都结束了!");
break;
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("主线程结束");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询