亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何保證Thread-1、2、3的順序執行

標簽:
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();
    }

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

點擊查看更多內容
TA 點贊

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

評論

作者其他優質文章

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

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消