自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 前端
在線程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)行順序。

目錄

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

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

通過join()方法使當(dāng)前線程“阻塞”,等待指定線程執(zhí)行完畢后繼續(xù)執(zhí)行。

舉例:在線程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)行順序。

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)槲覀冊(cè)谧泳€程中通過“join()方法”已經(jīng)指定了運(yùn)行順序。
        thread3.start();
        thread2.start();
        thread1.start();
 
    }
}

運(yùn)行結(jié)果:

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

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

簡(jiǎn)單說一下子線程與主線程的區(qū)別,子線程指的是發(fā)生在Thread內(nèi)部的代碼,主線程指的是發(fā)生在main函數(shù)中的代碼,我們可以在main函數(shù)中通過join()方法讓主線程阻塞等待以達(dá)到指定順序執(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)上冰箱!

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

CountDownLatch通過計(jì)數(shù)器提供了更靈活的控制,只要檢測(cè)到計(jì)數(shù)器為0當(dāng)前線程就可以往下執(zhí)行而不用管相應(yīng)的thread是否執(zhí)行完畢。

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é)果:

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

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

單線程化線程池(newSingleThreadExecutor)的優(yōu)點(diǎn),串行執(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)上冰箱!

責(zé)任編輯:武曉燕 來源: 一安未來
相關(guān)推薦

2017-04-17 19:31:03

Android多線程

2010-08-26 14:18:25

DIV高度

2014-03-17 09:22:43

Linux命令

2022-09-02 14:29:01

JavaScrip數(shù)組屬性

2018-10-10 14:02:39

前端JavaScript函數(shù)

2023-09-05 11:49:45

技術(shù)方式人工智能

2016-06-28 10:19:31

云計(jì)算云安全

2010-07-16 13:50:53

Perl哈希表

2009-11-23 15:57:51

PHP偽靜態(tài)

2021-03-10 10:13:39

爬蟲Python代碼

2023-02-03 08:47:20

職位招聘難題

2011-06-22 15:21:08

XML

2020-08-10 00:30:55

備份密碼iPhone移動(dòng)安全

2009-02-25 09:52:14

類型轉(zhuǎn)換.NET 強(qiáng)制轉(zhuǎn)型

2009-03-31 13:12:30

解析XMLJava

2022-03-14 09:05:37

Spring項(xiàng)目處理器

2015-09-10 09:30:54

Java多線程同步

2022-11-04 13:35:29

IT遠(yuǎn)程工作混合工作

2021-09-23 10:30:21

Docker RegiHarborLinux

2011-05-19 10:44:01

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)