作者:rsidugjig | 来源:互联网 | 2023-10-11 11:31
public class JoinTest {
1 2 3 4 5 6 7 8 9 10
| public static void main(String[] args) throws InterruptedException {
MyThread3 thread=new MyThread3();
thread.start();
//thread.join(1);//将主线程加入到子线程后面,不过如果子线程在1毫秒时间内没执行完,则主线程便不再等待它执行完,进入就绪状态,等待cpu调度
System.out.println(Thread.currentThread());
Thread.currentThread().join();
for(int i=0;i<30;i++){
System.out.println(Thread.currentThread().getName() + "线程第" + i + "次执行!");
}
} |
}
class MyThread3 extends Thread {
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Override
public void run() {
for (int i = 0; i <1000; i++) {
try {
System.out.println(this.getName() + "线程第" + i + "次执行!");
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
}