start 和 join

alluser 2003-05-09 02:07:41
java中thread类有start和join方法,有什么区别?
他们两个到底谁调用run方法?
...全文
80 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
alluser 2003-05-13
  • 打赏
  • 举报
回复
很奇怪
alluser 2003-05-09
  • 打赏
  • 举报
回复
没有人知道么?
alluser 2003-05-09
  • 打赏
  • 举报
回复
如果说是start调用run,在这段代码中,为什么去掉join就不调用run了呢?
public class Deadlock implements Runnable {
Deadlock grabIt;
public static void main(String args[]) {
Deadlock dlk1= new Deadlock();
Deadlock dlk2= new Deadlock();
Thread t1= new Thread (dlk1);
Thread t2= new Thread (dlk2);
dlk1.grabIt = dlk2;
dlk2.grabIt = dlk1;
t1.start();
t2.start();
System.out.println("Started");
try {
t1.join();
t2.join();
}
catch(InterruptedException e) {
System.out.println("error occurred");
}
System.exit(0);
}

public synchronized void run() {
try {
Thread.sleep(1500);
}
catch(InterruptedException e) {
System.out.println("error occurred");
}
System.out.println("run...");
grabIt.syncIt();
}
public synchronized void syncIt() {
try {
Thread.sleep(1500);
System.out.println("Sync");
}
catch(InterruptedException e) {
System.out.println("error occurred");
}
System.out.println("In the synchIt() method");
}
}// end class
xutaiqing520 2003-05-09
  • 打赏
  • 举报
回复
start调用run方法启用线程。
join方法带有一长整形参数,意为线程等待指定的时间后死亡。
wanglh2000 2003-05-09
  • 打赏
  • 举报
回复
start调用run方法

62,615

社区成员

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

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