设为首页 加入收藏

TOP

java实现子线程全部结束主线程继续执行
2014-11-23 23:31:57 来源: 作者: 【 】 浏览:5
Tags:java 实现 线程 全部 结束 继续 执行

在主线程中启动多个子线程 要让主线程等待所有子线程都结束才能继续执行


代码如下
public class Test {


/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
mainThread mt = t.new mainThread();
mt.run();
}


class mainThread implements Runnable {


@Override
public void run() {
System.out.println(“main begin”);
childThread[] cts = new childThread[200];
for(int i = 0;i<200;i++){
childThread ct = new childThread();
ct.start();
cts = ct;
}


try {
for(int i = 0;i<200;i++){
cts.join();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


System.out.println(“main end”);
}


}


class childThread extends Thread implements Runnable {


@Override
public void run() {
String str = “啊”;
for (int i = 0; i < 1000; i++) {
str += “!”;
}


System.out.println(“child thread end”);
}


}
}


输出结果


main begin
child thread end
child thread end
…200次
child thread end
main end


实际上就是用到了Thread类的join方法


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇按bug严重性分类可将BUG分为那些.. 下一篇德勤面试:咨询部门。

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: