如何控制多线程执行顺序
方法一:Join()使用
先看一段代码:
1 2 3 4 5 6 7 8 9 10 11 12
   | package main.java;
  public class App {
    static Thread thread1 = new Thread(new Runnable(){
        @Override       public void run() {       System.out.println("thread1");       }
        });
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   |   static Thread thread2 = new Thread(new Runnable(){
        @Override       public void run() {
        System.out.println("thread2");       }
        });
    static Thread thread3 = new Thread(new Runnable(){
        @Override       public void run() {       System.out.println("thread3");
        }
        });
    public static void main(String[] args) throws InterruptedException{
        thread1.start();       thread1.join();
        thread2.start();       thread2.join();
        thread3.start();
    }
  }
  | 
 
运行结果如下:
我们不管执行多少次都是按顺序执行的。
原理分析:
Join()作用:让主线程等待子线程运行结束后才能继续运行。
这段代码里面的意思是这样的:
程序在main线程中调用thread1线程的join方法,则main线程放弃cpu控制权,并返回thread1线程继续执行直到线程thread1执行完毕
所以结果是thread1线程执行完后,才到主线程执行,相当于在main线程中同步thread1线程,thread1执行完了,main线程才有执行的机会.
我们来看看join()的源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   | /** * Waits at most <code>millis</code> milliseconds for this thread to * die. A timeout of <code>0</code> means to wait forever. */
  public final synchronized void join(long millis)   throws InterruptedException {   long base = System.currentTimeMillis();   long now = 0;
    if (millis < 0) {     throw new IllegalArgumentException("timeout value is negative");   }
    if (millis == 0) {     while (isAlive()) {       wait(0);     }   } else {     while (isAlive()) {       long delay = millis - now;       if (delay <= 0) {         break;       }       wait(delay);       now = System.currentTimeMillis() - base;     }   } }
   | 
 
源码解读:
这里有一个isAlive()方法很重要。什么意思呢?
判断当前线程是否处于活动状态。活动状态就是线程启动且尚未终止,比如正在运行或准备开始运行。
所以从代码上看,如果线程被生成了,但还未被起动,调用它的 join() 方法是没有作用的,将直接继续向下执行。
wait()方法,什么意思呢?
在Object.java中,wait()的作用是让当前线程进入等待状态,同时,wait()也会让当前线程释放它所持有的锁。
所以Join()主要就是通过wait()方法来实现这个目的的。
最后来个代码步骤解读吧:
1: 主线程运行;
2:创建thread1线程 (创建后的thread1线程状态为新建状态);
3:主线程调用thread1.start()方法 (thread1线程状态变为就绪状态,等待cpu的一个资源调度,有了资源后thread1状态为运行状态);
4:主线程调用thread1.join() 方法 (主线程会休眠,等待子线程thread1运行结束后才会继续运行)。
方法二:ExecutorService ()的使用
依旧先看代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   | package main.java;
  import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
  public class App {
  static Thread thread1 = new Thread(new Runnable(){
      @Override     public void run() {     System.out.println("thread1");   }
  });
 
  static Thread thread2 = new Thread(new Runnable(){
      @Override     public void run() {
      System.out.println("thread2");     }
  });
  static Thread thread3 = new Thread(new Runnable(){
      @Override     public void run() {     System.out.println("thread3");
      }
  });
  static ExecutorService executorService = Executors.newSingleThreadExecutor();
  public static void main(String[] args) throws InterruptedException{
      executorService.submit(thread1);     executorService.submit(thread2);     executorService.submit(thread3);
      executorService.shutdown(); }
  }
   | 
 
运行结果如下:
结果:无论运行多少次,结果都是按照我们的顺序执行的。
原理:利用并发包里的Excutors的newSingleThreadExecutor产生一个单线程的线程池,而这个线程池的底层原理就是一个先进先出(FIFO)的队列。代码中executor.submit依次添加了123线程,按照FIFO的特性,执行顺序也就是123的执行结果,从而保证了执行顺序。