JAVA中的合并线程join()方法 void join() thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。 比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。t.join(); //调用join方法,等待线程t执行完毕 t.join(1000); //等待 t 线程,等待时间是1000毫秒。 public class ThreadScheduleTest03 { public static void main(String[] args) { Thread thread = new Thread(new MyRunnable7()); thread.start(); for (int i = 0; i < 1000; i++) { //当main=500时,插入thread线程,直到thread线程运行结束,main线程才继续执行 if (i == 500){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "——>" + i); } } } class MyRunnable7 implements Runnable{ @Override public void run() { for (int i = 1; i <= 1000; i++) { System.out.println(Thread.currentThread().getName() + "——>" + i); } } }