线程join()使用失败问题
代码如下:
-----------------------------------------------------------------------------------------
public class Test3 {
public static void main(String[] args) {
Thread t1=new Thread(new A(), "No1");
Thread t2=new Thread(new B(), "No2");
t1.start();
t2.start();
try {
t1.join(); //为什么没有达到t1独自占用的效果?
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class A implements Runnable {
public void run() {
for (int i=1; i<=10; ++i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
class B implements Runnable {
public void run() {
for (int i=1; i<=10; ++i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}