线程join()使用失败问题

qq_31134691 2015-10-06 11:07:29
代码如下:
-----------------------------------------------------------------------------------------
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);
}
}
}
...全文
58 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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