因为你循环中每次都是new了一个新的thread,join应该是join一个已经正在运行的,但是你join了一个刚new但是还没开始运行的,使用方法有错,所以11111在最上面的打印了,因为没有找到需要等待执行完毕的线程 这是我写了一个demo,你看看,分别去掉t1和t2的join会有什么变化 public class TestJoin { public static void main(String[] args) { TestJoin testJoin = new TestJoin(); try { ThreadProcess t1 =testJoin.new ThreadProcess(1); ThreadProcess t2 =testJoin.new ThreadProcess(2); t1.start(); t1.join(); t2.start(); t2.join(); } catch (Exception e) { e.printStackTrace(); } System.out.println("1111111111"); } class ThreadProcess extends Thread { private int mm; public ThreadProcess(int mm){ this.mm = mm; } public void run() { for (int i = 0; i < 5; i++) { System.out.println(mm); } } } }
[quote=引用 1 楼 CatWitch 的回复:] 因为你循环中每次都是new了一个新的thread,join应该是join一个已经正在运行的,但是你join了一个刚new但是还没开始运行的,使用方法有错,所以11111在最上面的打印了,因为没有找到需要等待执行完毕的线程 这是我写了一个demo,你看看,分别去掉t1和t2的join会有什么变化 public class TestJoin { public static void main(String[] args) { TestJoin testJoin = new TestJoin(); try { ThreadProcess t1 =testJoin.new ThreadProcess(1); ThreadProcess t2 =testJoin.new ThreadProcess(2); t1.start(); t1.join(); t2.start(); t2.join(); } catch (Exception e) { e.printStackTrace(); } System.out.println("1111111111"); } class ThreadProcess extends Thread { private int mm; public ThreadProcess(int mm){ this.mm = mm; } public void run() { for (int i = 0; i < 5; i++) { System.out.println(mm); } } } }
81,092
社区成员
341,717
社区内容
加载中
试试用AI创作助手写篇文章吧