溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

三個線程順序執(zhí)行的實現(xiàn)方法是什么

發(fā)布時間:2021-10-26 17:10:34 來源:億速云 閱讀:228 作者:iii 欄目:web開發(fā)

本篇內(nèi)容主要講解“三個線程順序執(zhí)行的實現(xiàn)方法是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“三個線程順序執(zhí)行的實現(xiàn)方法是什么”吧!

先說下要求,就是三個線程,假設(shè)是線程 1,2,3, 現(xiàn)在的要求是:必須是線程 1 先執(zhí)行,然后線程 2 再執(zhí)行,最后是線程 3 執(zhí)行,然后有幾種實現(xiàn)方法呢?

其實它的本質(zhì)就是實現(xiàn),讓線程 2,3 等待線程 1 執(zhí)行完畢,所以重點就是有哪些方法可以讓線程 2,3 等待。

join

第一反應(yīng)應(yīng)該就是使用 join 方法,因為 join 本來就是支持這種機制的

比如,我在線程 B 中調(diào)用了線程 A 的 join 方法,那么線程 B 就會等線程 A 執(zhí)行結(jié)束之后再執(zhí)行

那么具體應(yīng)該怎么使用嘞?

別慌嘛,我這里有例子,你瞅瞅:

public class ThreadLoopOne {     public static void main(String[] args) {         Thread t1 = new Thread(new Work(null));         Thread t2 = new Thread(new Work(t1));         Thread t3 = new Thread(new Work(t2));          t1.start();         t2.start();         t3.start();     }      static class Work implements Runnable {         private Thread beforeThread;         public Work(Thread beforeThread){             this.beforeThread = beforeThread;         }          @Override         public void run() {             // 如果有線程,就 join 進來,沒有的話就直接輸出             if (beforeThread != null ){                 try {                     beforeThread.join();                     System.out.println("thread start : " + Thread.currentThread().getName());                 } catch (InterruptedException e) {                     e.printStackTrace();                 }             }else{                 System.out.println("thread start : " + Thread.currentThread().getName());             }         }     } }

CountDownLatch

剛才說了,本質(zhì)就是讓線程 B,C 等待線程 A 執(zhí)行完畢

那么信號量就是一個不錯的選擇

如果想要實現(xiàn)的話,那大概就是下面這樣:

public class ThreadLoopTwo {     public static void main(String[] args) {         // 設(shè)置線程 1 的信號量為 0         CountDownLatch cOne = new CountDownLatch(0);         // 設(shè)置線程 2 的信號量為 1         CountDownLatch cTwo = new CountDownLatch(1);         // 設(shè)置線程 3 的信號量為 1         CountDownLatch cThree = new CountDownLatch(1);          // 因為 cOne 為 0 ,故 t1 可以直接執(zhí)行         Thread t1 = new Thread(new Work(cOne,cTwo));         // 線程 t1 執(zhí)行完畢之后,此時的 cTwo 為 0 , t2 開始執(zhí)行         Thread t2 = new Thread(new Work(cTwo,cThree));         // 線程 t2 執(zhí)行完畢,此時 cThree 為 0 , t3 開始執(zhí)行         Thread t3 = new Thread(new Work(cThree,cThree));          t1.start();         t2.start();         t3.start();     }      static class Work implements Runnable{         CountDownLatch cOne;         CountDownLatch cTwo;          public Work(CountDownLatch cOne, CountDownLatch cTwo){             super();             this.cOne = cOne;             this.cTwo = cTwo;         }         @Override         public void run() {             try {                 // 當前一個線程信號量為 0 時,才執(zhí)行                 cOne.await();                 System.out.println("thread start : " + Thread.currentThread().getName());                 // 后一個線程信號量減 1                 cTwo.countDown();             } catch (InterruptedException e) {                 e.printStackTrace();             }         }     } }

使用單個線程池

之所以線程 1,2,3 的執(zhí)行順序無法保證,是因為在編譯器可能會去做一些優(yōu)化,導致沒有辦法按照順序執(zhí)行

如果我們使用單個線程池去執(zhí)行的話,那就沒有這樣的問題了

具體實現(xiàn):

public class ThreadLoopThree {     public static void main(String[] args) {         Thread t1 = new Thread(new Runnable() {             @Override             public void run() {                 System.out.println("thread start : " + Thread.currentThread().getName() + " run one");             }         });          Thread t2 = new Thread(new Runnable() {             @Override             public void run() {                 System.out.println("thread start : " + Thread.currentThread().getName() + " run two");             }         });          Thread t3 = new Thread(new Runnable() {             @Override             public void run() {                 System.out.println("thread start : " + Thread.currentThread().getName() + " run three");             }         });          ExecutorService executor = Executors.newSingleThreadExecutor();         // 將線程依次加入到線程池中         executor.submit(t1);         executor.submit(t2);         executor.submit(t3);         // 及時將線程池關(guān)閉         executor.shutdown();     } }

CompletableFuture

如果使用 CompletableFuture 來實現(xiàn)的話,代碼就非常簡潔了

public class ThreadLoopFour {     public static void main(String[] args)  {         Thread t1 = new Thread(new Work());         Thread t2 = new Thread(new Work());         Thread t3 = new Thread(new Work());          CompletableFuture.runAsync(()-> t1.start())                 .thenRun(()->t2.start())                 .thenRun(()->t3.start());     }      static class Work implements Runnable{         @Override         public void run() {             System.out.println("thread start : " + Thread.currentThread().getName());         }     } }

到此,相信大家對“三個線程順序執(zhí)行的實現(xiàn)方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI