第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

如何保證Thread-1、2、3的順序執(zhí)行

標(biāo)簽:
Java

/
有三个线程T1 T2 T3,如何保证他们按顺序执行-转载
在T2的run中,调用t1.join,让t1执行完成后再让T2执行
在T2的run中,调用t2.join,让t2执行完成后再让T3执行
/

方法一:

public class Test {
    private int count=2;

    public static void main(String[] args) {
        try {
            new Test().preserveOrderViaJoin();
        }catch (Exception e){
            System.out.println("error!");
        }
    }

    private void preserveOrderViaJoin() throws InterruptedException {
        Thread tmp;
        System.out.println("start!");
        for (int i=0;i<count;i++){
            tmp=new Thread(new Runnable() {
                public void run() {
                    System.out.println("Thread.currentThread().getName():"+Thread.currentThread().getName());
                }
            },"Thread"+i);
            tmp.start();
            tmp.join();
        }
        System.out.println("end!");
    }

}

方法二:

public void testCountDownLatch(){
        final CountDownLatch countDownLatch = new CountDownLatch(0);
        final CountDownLatch countDownLatch1 = new CountDownLatch(1);
        final CountDownLatch countDownLatch2 = new CountDownLatch(1);

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                try {
                    countDownLatch.await();
                    System.out.println(Thread.currentThread().getName()+"start");
                    System.out.println(Thread.currentThread().getName()+"end");
                    countDownLatch1.countDown();
                } catch (Exception e){
                    System.out.println(Thread.currentThread().getName()+"exception!");
                }
            }
        },"Thread-1");

        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                try {
                    countDownLatch1.await();
                    System.out.println(Thread.currentThread().getName()+"start");
                    System.out.println(Thread.currentThread().getName()+"end");
                    countDownLatch2.countDown();
                } catch (Exception e){
                    System.out.println(Thread.currentThread().getName()+"exception!");
                }
            }
        },"Thread-2");

        Thread thread3 = new Thread(new Runnable() {
            public void run() {
                try {
                    countDownLatch2.await();
                    System.out.println(Thread.currentThread().getName()+"start");
                    System.out.println(Thread.currentThread().getName()+"end");
                } catch (Exception e){
                    System.out.println(Thread.currentThread().getName()+"exception!");
                }
            }
        },"Thread-3");

        thread1.start();
        thread2.start();
        thread3.start();
    }

根据网络上相应内容做出的整合

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消