我是靠谱客的博主 幸福枕头,这篇文章主要介绍线程的强制运行:join()方法,现在分享给大家,希望可以做个参考。

/*线程的强制运行:join()方法*/
class MyThread4 implements Runnable{
public void run(){
for(int i=0; i<15; i++){
System.out.println(Thread.currentThread().getName()+"运行"+i);
}
}
}
public class ThreadJoin {


public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread4 mt = new MyThread4();
Thread th = new Thread(mt,"线程A");
th.start();
for(int i=0; i<25; i++){
if(i>7){
try {
th.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("xiancheng"+i);
}
}


}






/*
运行结果:
xiancheng0
xiancheng1
xiancheng2
xiancheng3
xiancheng4
xiancheng5
xiancheng6
xiancheng7
线程A运行0
线程A运行1
线程A运行2
线程A运行3
线程A运行4
线程A运行5
线程A运行6
线程A运行7
线程A运行8
线程A运行9
线程A运行10
线程A运行11
线程A运行12
线程A运行13
线程A运行14
xiancheng8
xiancheng9
xiancheng10
xiancheng11
xiancheng12
xiancheng13
xiancheng14
xiancheng15
xiancheng16
xiancheng17
xiancheng18
xiancheng19
xiancheng20
xiancheng21
xiancheng22
xiancheng23
xiancheng24


*/

最后

以上就是幸福枕头最近收集整理的关于线程的强制运行:join()方法的全部内容,更多相关线程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(141)

评论列表共有 0 条评论

立即
投稿
返回
顶部