您的位置:首页 > 其它

Thread嵌套Thread

2017-03-08 00:00 417 查看
一个线程Thread1嵌套线程Thread2,使用Thread1.join()不会在Thread2完成之后继续执行。

public class Test1 {
private static Thread t4;
static Thread t1 = new Thread(){
@Override
public void run(){
System.out.println("1");
}
};
static Thread t2 = new Thread(){
@Override
public void run(){
try {
t4 = new Thread(){
@SuppressWarnings("static-access")
@Override
public void run(){
try {
t4.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("4");
}
};
t4.start();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("2");
}
};
public static void main(String[] args){
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.start();

}
}

执行结果

2
1
4

t1只会让t2执行完毕,不会等待t2内部的t4执行完毕。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Thread 执行顺序