package thread;
/**
* join方法
* 加入线程,让调用的线程先执行指定时间或执行完毕
*/
public class ThreadDemo2 {
public static void main(String[] args) {
MyRunnable2 myRunnable2 = new MyRunnable2();
Thread t = new Thread(myRunnable2);
t.start();
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"--------------------------"+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i==20){
try {
t.join();//让t线程先执行完毕
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class MyRunnable2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName()+"---"+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
java线程join方法
最新推荐文章于 2025-06-22 12:23:47 发布
7907

被折叠的 条评论
为什么被折叠?



