日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

掌握這四種方法,多線程按序執(zhí)行不再是問題

來源: 責(zé)編: 時(shí)間:2024-06-27 17:20:27 134觀看
導(dǎo)讀目錄在子線程中通過join()方法指定順序在主線程中通過join()方法指定順序通過倒數(shù)計(jì)時(shí)器CountDownLatch實(shí)現(xiàn)通過創(chuàng)建單一化線程池newSingleThreadExecutor()實(shí)現(xiàn)在子線程中通過join()方法指定順序通過join()方法使當(dāng)

目錄

  • 在子線程中通過join()方法指定順序
  • 在主線程中通過join()方法指定順序
  • 通過倒數(shù)計(jì)時(shí)器CountDownLatch實(shí)現(xiàn)
  • 通過創(chuàng)建單一化線程池newSingleThreadExecutor()實(shí)現(xiàn)

在子線程中通過join()方法指定順序

通過join()方法使當(dāng)前線程“阻塞”,等待指定線程執(zhí)行完畢后繼續(xù)執(zhí)行。4I228資訊網(wǎng)——每日最新資訊28at.com

舉例:在線程thread2中,加上一句thread1.join(),其意義在于,當(dāng)前線程2運(yùn)行到此行代碼時(shí)會(huì)進(jìn)入阻塞狀態(tài),直到線程thread1執(zhí)行完畢后,線程thread2才會(huì)繼續(xù)運(yùn)行,這就保證了線程thread1與線程thread2的運(yùn)行順序。4I228資訊網(wǎng)——每日最新資訊28at.com

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)整,程序運(yùn)行結(jié)果不受影響,因?yàn)槲覀冊谧泳€程中通過“join()方法”已經(jīng)指定了運(yùn)行順序。        thread3.start();        thread2.start();        thread1.start();     }}

運(yùn)行結(jié)果:4I228資訊網(wǎng)——每日最新資訊28at.com

打開冰箱!拿出一瓶牛奶!關(guān)上冰箱!

在主線程中通過join()方法指定順序

簡單說一下子線程與主線程的區(qū)別,子線程指的是發(fā)生在Thread內(nèi)部的代碼,主線程指的是發(fā)生在main函數(shù)中的代碼,我們可以在main函數(shù)中通過join()方法讓主線程阻塞等待以達(dá)到指定順序執(zhí)行的目的。4I228資訊網(wǎng)——每日最新資訊28at.com

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é)果:4I228資訊網(wǎng)——每日最新資訊28at.com

打開冰箱!拿出一瓶牛奶!關(guān)上冰箱!

通過倒數(shù)計(jì)時(shí)器CountDownLatch實(shí)現(xiàn)

CountDownLatch通過計(jì)數(shù)器提供了更靈活的控制,只要檢測到計(jì)數(shù)器為0當(dāng)前線程就可以往下執(zhí)行而不用管相應(yīng)的thread是否執(zhí)行完畢。4I228資訊網(wǎng)——每日最新資訊28at.com

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("關(guān)上冰箱!");                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        });         //下面三行代碼順序可隨意調(diào)整,程序運(yùn)行結(jié)果不受影響        thread3.start();        thread1.start();        thread2.start();    }}

輸出結(jié)果:4I228資訊網(wǎng)——每日最新資訊28at.com

打開冰箱!拿出一瓶牛奶!關(guān)上冰箱!

通過創(chuàng)建單一化線程池newSingleThreadExecutor()實(shí)現(xiàn)

單線程化線程池(newSingleThreadExecutor)的優(yōu)點(diǎn),串行執(zhí)行所有任務(wù)。4I228資訊網(wǎng)——每日最新資訊28at.com

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é)果:4I228資訊網(wǎng)——每日最新資訊28at.com

打開冰箱!拿出一瓶牛奶!關(guān)上冰箱!

4I228資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-96998-0.html掌握這四種方法,多線程按序執(zhí)行不再是問題

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: Library Cache Hash Bucket與共享池閂鎖爭用問題

下一篇: Python自動(dòng)化:適合新手練習(xí)的五個(gè)有趣又實(shí)用的Python腳本,幫你快速掌握編程技能!拿走不謝!

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 普陀区| 嵊泗县| 互助| 邳州市| 尉氏县| 吉木乃县| 安丘市| 曲靖市| 枝江市| 专栏| 井冈山市| 岚皋县| 通化市| 肃南| 陇西县| 桂东县| 嘉荫县| 肇东市| 拜城县| 黑河市| 涿州市| 郴州市| 宣城市| 如皋市| 高陵县| 安福县| 宜黄县| 饶河县| 武隆县| 武城县| 伽师县| 寻乌县| 威海市| 鹿泉市| 鹰潭市| 会泽县| 漳平市| 伽师县| 南召县| 宜兰县| 高平市|