关于Java的join方法

woxiaomaa 2018-01-19 11:38:26
package com.ma.third.join;

public class Thread1 extends Thread {

@Override
public void run() {
super.run();
synchronized(this){
try {
System.out.println("t1 begin");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1 end");
}
}


}


package com.ma.third.join;

public class MyMain1 {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.setName("t1");
t1.start();
try {
t1.join(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("main end");

}

}


请问大佬们:join方法是通过wait方法实现的,那么调用谁的wait?是t1线程的wait还是主线程的wait?如果是调用t1线程的wait,那么程序有时如何notify主线程的
...全文
781 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_39670158 2018-01-20
  • 打赏
  • 举报
回复
join方法点进去 执行如下代码 while (isAlive()) {/Tests if this thread is alive. A thread is alive if it has been started and has not yet died. wait(0); } 大胆猜测:wait(0)大概意思是 0毫秒后该线程开始执行,配合while语句 相当于让执行join方法的线程一直跑直到(isAlive())为false

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧