1v1影片原始碼,你知道如何實現多執行緒的順序執行嗎?
透過join()方法使當前執行緒“阻塞”,等待指定執行緒執行完畢後繼續執行。
舉例:線上程thread2中,加上一句thread1.join(),其意義在於,當前執行緒2執行到此行程式碼時會進入阻塞狀態,直到執行緒thread1執行完畢後,執行緒thread2才會繼續執行,這就保證了執行緒thread1與執行緒thread2的執行順序。
public class ThreadJoinDemo { public static void main(String[] args) throws InterruptedException { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("開啟冰箱!"); } }); final Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { thread1.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("拿出一瓶牛奶!"); } }); final Thread thread3 = new Thread(new Runnable() { @Override public void run() { try { thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("關上冰箱!"); } }); //下面三行程式碼順序可隨意調整,程式執行結果不受影響,因為我們在子執行緒中透過“join()方法”已經指定了執行順序。 thread3.start(); thread2.start(); thread1.start(); } }
執行結果:
開啟冰箱!
拿出一瓶牛奶!
關上冰箱!
簡單說一下1v1影片原始碼中子執行緒與主執行緒的區別,子執行緒指的是發生在Thread內部的程式碼,主執行緒指的是發生在main函式中的程式碼,我們可以在main函式中透過join()方法讓主執行緒阻塞等待以達到指定順序執行的目的。
public class ThreadMainJoinDemo { public static void main(String[] args) throws InterruptedException { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("開啟冰箱!"); } }); final Thread thread2 = new Thread(new Runnable() { @Override public void run() { System.out.println("拿出一瓶牛奶!"); } }); final Thread thread3 = new Thread(new Runnable() { @Override public void run() { System.out.println("關上冰箱!"); } }); thread1.start(); thread1.join(); thread2.start(); thread2.join(); thread3.start(); } }
輸出結果:
開啟冰箱!
拿出一瓶牛奶!
關上冰箱!
CountDownLatch透過計數器提供了更靈活的控制,只要檢測到計數器為0當前執行緒就可以往下執行而不用管相應的thread是否執行完畢。
public class ThreadCountDownLatchDemo { private static CountDownLatch countDownLatch1 = new CountDownLatch(1); private static CountDownLatch countDownLatch2 = new CountDownLatch(1); public static void main(String[] args) { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("開啟冰箱!"); countDownLatch1.countDown(); } }); final Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { countDownLatch1.await(); System.out.println("拿出一瓶牛奶!"); countDownLatch2.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }); final Thread thread3 = new Thread(new Runnable() { @Override public void run() { try { countDownLatch2.await(); System.out.println("關上冰箱!"); } catch (InterruptedException e) { e.printStackTrace(); } } }); //下面三行程式碼順序可隨意調整,程式執行結果不受影響 thread3.start(); thread1.start(); thread2.start(); } }
輸出結果:
開啟冰箱!
拿出一瓶牛奶!
關上冰箱!
單執行緒化執行緒池(newSingleThreadExecutor)的優點,序列執行所有任務。
public class ThreadPoolDemo { static ExecutorService executorService = Executors.newSingleThreadExecutor(); public static void main(String[] args) { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("開啟冰箱!"); } }); final Thread thread2 =new Thread(new Runnable() { @Override public void run() { System.out.println("拿出一瓶牛奶!"); } }); final Thread thread3 = new Thread(new Runnable() { @Override public void run() { System.out.println("關上冰箱!"); } }); executorService.submit(thread1); executorService.submit(thread2); executorService.submit(thread3); executorService.shutdown(); //使用完畢記得關閉執行緒池 } }
輸出結果:
開啟冰箱!
拿出一瓶牛奶!
關上冰箱!
以上就是1v1影片原始碼,你知道如何實現多執行緒的順序執行嗎?, 更多內容歡迎關注之後的文章