您的位置:首页 > 其它

现有三个线程T1,T2,T3 三个线程依次执行

2017-10-09 17:52 477 查看

直接上代码

package test;

public class ThreadJoin {
public static void main(String[] args) {
thread3.start();
thread2.start();
thread1.start();
}

static Thread thread1 = new Thread(new Runnable() {

@Override
public void run() {
System.out.println("t1");
}
});
static Thread thread2 = new Thread(new Runnable() {

@Override
public void run() {
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t2");
}
});
static Thread thread3 = new Thread(new Runnable() {

@Override
public void run() {
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t3");
}
});
}


执行结果

t1
t2
t3


thread的join方法其实底层就是调用的object的wait与notify机制

t2里面调用t1.join(),其实调用的是t1里面的wait方法

在t1执行完毕之后t1调用notify通知t2可以继续执行

下面是一个详细的连接

wait与notify
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐