Thread类的join方法测试:
package thread.test03;
public class ThreadJoinTest {
public static void main(String[] args) {
Thread tt = new TestThread();
tt.start();
int index = 0;
while(true){
if(index++ == 100){
try {
tt.join();//join():等待tt线程终止,再接着执行当前线程;join(long millis):指定一定的时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("main:"+Thread.currentThread().getName());
}
}
}
class TestThread extends Thread{
@Override
public void run() {
while(true){
System.out.println("run:"+Thread.currentThread().getName());
}
}
}
本文通过一个具体的示例代码展示了Java中Thread类的join方法如何使用。join方法可以使当前线程等待另一个线程终止后再继续执行。代码示例中创建了一个名为TestThread的线程,并在主线程中调用了该线程的join方法,演示了join方法的基本用法。
686

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



