通过join方法阻塞其它线程,等待该线程执行完毕。 package cn.mym.thread;public class TestJoin {public static void main(String[] args) {Thread5 thread = new Thread5();thread.start();int i = 0;while(i10){try {Thread.sleep(50
package cn.mym.thread; public class TestJoin { public static void main(String[] args) { Thread5 thread = new Thread5(); thread.start(); int i = 0; while(i<10){ try { Thread.sleep(50); if(i == 5){ thread.join(); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("world"); i++; } } } class Thread5 extends Thread{ @Override public void run() { int i = 0; while(i<10){ try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Thread4"); i++; } } } //====================执行结果================= world Thread4 Thread4 world Thread4 world Thread4 world Thread4 world Thread4 Thread4 Thread4 Thread4 Thread4 world world world world world