为了账号安全,请及时绑定邮箱和手机立即绑定

如何保证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下载
官方微信

举报

0/150
提交
取消