您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Java怎么讓多線程按順序執(zhí)行”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Java怎么讓多線程按順序執(zhí)行”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
通過join()方法使當前線程“阻塞”,等待指定線程執(zhí)行完畢后繼續(xù)執(zhí)行。舉例:在線程thread2中,加上一句thread1.join(),其意義在于,當前線程2運行到此行代碼時會進入阻塞狀態(tài),直到線程thread1執(zhí)行完畢后,線程thread2才會繼續(xù)運行,這就保證了線程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("關(guān)上冰箱!"); } }); //下面三行代碼順序可隨意調(diào)整,程序運行結(jié)果不受影響,因為我們在子線程中通過“join()方法”已經(jīng)指定了運行順序。 thread3.start(); thread2.start(); thread1.start(); } }
運行結(jié)果:
打開冰箱!
拿出一瓶牛奶!
關(guān)上冰箱!
簡單說一下子線程與主線程的區(qū)別,子線程指的是發(fā)生在Thread內(nèi)部的代碼,主線程指的是發(fā)生在main函數(shù)中的代碼,我們可以在main函數(shù)中通過join()方法讓主線程阻塞等待以達到指定順序執(zhí)行的目的。
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("關(guān)上冰箱!"); } }); thread1.start(); thread1.join(); thread2.start(); thread2.join(); thread3.start(); } }
輸出結(jié)果:
打開冰箱!
拿出一瓶牛奶!
關(guān)上冰箱!
CountDownLatch通過計數(shù)器提供了更靈活的控制,只要檢測到計數(shù)器為0當前線程就可以往下執(zhí)行而不用管相應(yīng)的thread是否執(zhí)行完畢。
public class ThreadCountDownLatchDemo { private static CountDownLatch countDownLatch2 = new CountDownLatch(1); private static CountDownLatch countDownLatch3 = new CountDownLatch(1); public static void main(String[] args) { final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("打開冰箱!"); countDownLatch2.countDown(); } }); final Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { countDownLatch2.await(); System.out.println("拿出一瓶牛奶!"); countDownLatch3.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }); final Thread thread3 = new Thread(new Runnable() { @Override public void run() { try { countDownLatch3.await(); System.out.println("關(guān)上冰箱!"); } catch (InterruptedException e) { e.printStackTrace(); } } }); //下面三行代碼順序可隨意調(diào)整,程序運行結(jié)果不受影響 thread3.start(); thread1.start(); thread2.start(); } }
輸出結(jié)果:
打開冰箱!
拿出一瓶牛奶!
關(guān)上冰箱!
單線程化線程池(newSingleThreadExecutor)的優(yōu)點,串行執(zhí)行所有任務(wù)。
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("關(guān)上冰箱!"); } }); executorService.submit(thread1); executorService.submit(thread2); executorService.submit(thread3); executorService.shutdown(); //使用完畢記得關(guān)閉線程池 } }
輸出結(jié)果:
打開冰箱!
拿出一瓶牛奶!
關(guān)上冰箱!
讀到這里,這篇“Java怎么讓多線程按順序執(zhí)行”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(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)容。