多线程顺序运行的 4 种方法

1、在子线程中通过join()方法指定顺序

通过join()方法使当前线程“阻塞”,等待指定线程执行完毕后继续执行。

举例:在线程thread2中,加上一句thread1.join(),其意义在于,当前线程2运行到此行代码时会进入阻塞状态,直到线程thread1执行完毕后,线程thread2才会继续运行,这就保证了线程thread1与线程thread2的运行顺序。

复制代码
public class ThreadJoinDemo {
    public static void main(String[] args) {
// 下面三行代码顺序可随意调整,程序运行结果不受影响,因为我们在子线程中通过“join()方法”已经指定了运行顺序。 thread3.start(); thread2.start(); thread1.start(); } static final Thread thread1 = new Thread(new Runnable() { @Override public void run() { System.out.println("打开冰箱!!!"); } }); static final Thread thread2 = new Thread(new Runnable() { @Override public void run() { try{ thread1.join(); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("拿出一瓶牛奶!!!"); } }); static final Thread thread3 = new Thread(new Runnable() { @Override public void run() { try{ thread2.join(); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("关上冰箱!!!"); } }); }
// 运行结果:
打开冰箱!!!
拿出一瓶牛奶!!!
关上冰箱!!!
复制代码

2、在主线程中通过join()方法指定顺序

简单说一下子线程与主线程的区别,子线程指的是发生在Thread内部的代码,主线程指的是发生在main函数中的代码。

我们可以在main函数中通过join()方法让主线程阻塞等待以达到指定顺序执行的目的。

复制代码
public class ThreadJoinDemo {
    public static void main(String[] args) throws InterruptedException {
        thread1.start();
        thread1.join();
        thread2.start();
        thread2.join();
        thread3.start();
    }
    static final Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("打开冰箱!!!");
        }
    });
    static final Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("拿出一瓶牛奶!!!");
        }
    });
    static final Thread thread3 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("关上冰箱!!!");
        }
    });
}
// 输出结果:
打开冰箱!!!
拿出一瓶牛奶!!!
关上冰箱!!!
复制代码

3、通过倒数计时器CountDownLatch实现

CountDownLatch 通过计数器提供了更灵活的控制,只要检测到计数器为 0 当前线程就可以往下执行而不用管相应的 thread 是否执行完毕。

复制代码
public class ThreadCountDownLatchDemo {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> CountDownLatch countDownLatch1 = <span style="color: rgba(0, 0, 255, 1)">new</span> CountDownLatch(1<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> CountDownLatch countDownLatch2 = <span style="color: rgba(0, 0, 255, 1)">new</span> CountDownLatch(1<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    </span><span style="color: rgba(0, 0, 255, 1)">final</span> Thread thread1 = <span style="color: rgba(0, 0, 255, 1)">new</span> Thread(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            System.out.println(</span>"打开冰箱!"<span style="color: rgba(0, 0, 0, 1)">);
            <span style="color: rgba(255, 0, 0, 1)">countDownLatch1.countDown();</span>
        }
    });

    </span><span style="color: rgba(0, 0, 255, 1)">final</span> Thread thread2 = <span style="color: rgba(0, 0, 255, 1)">new</span> Thread(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                <span style="color: rgba(255, 0, 0, 1)">countDownLatch1.await();</span>
                System.out.println(</span>"拿出一瓶牛奶!"<span style="color: rgba(0, 0, 0, 1)">);
                <span style="color: rgba(255, 0, 0, 1)">countDownLatch2.countDown();</span>
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    </span><span style="color: rgba(0, 0, 255, 1)">final</span> Thread thread3 = <span style="color: rgba(0, 0, 255, 1)">new</span> Thread(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                <span style="color: rgba(255, 0, 0, 1)">countDownLatch2.await();</span>
                System.out.println(</span>"关上冰箱!"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">下面三行代码顺序可随意调整,程序运行结果不受影响</span>

thread3.start();
thread1.start();
thread2.start();
}
}
// 输出结果:
打开冰箱!
拿出一瓶牛奶!
关上冰箱!

复制代码

4、通过创建单一化线程池 newSingleThreadExecutor()实现

单线程化线程池 ( newSingleThreadExecutor ) 的优点,串行执行所有任务。

复制代码
public class ThreadPoolDemo {

static ExecutorService executorService = Executors.newSingleThreadExecutor();

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    </span><span style="color: rgba(0, 0, 255, 1)">final</span> Thread thread1 = <span style="color: rgba(0, 0, 255, 1)">new</span> Thread(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            System.out.println(</span>"打开冰箱!"<span style="color: rgba(0, 0, 0, 1)">);
        }
    });

    </span><span style="color: rgba(0, 0, 255, 1)">final</span> Thread thread2 =<span style="color: rgba(0, 0, 255, 1)">new</span> Thread(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            System.out.println(</span>"拿出一瓶牛奶!"<span style="color: rgba(0, 0, 0, 1)">);
        }
    });

    </span><span style="color: rgba(0, 0, 255, 1)">final</span> Thread thread3 = <span style="color: rgba(0, 0, 255, 1)">new</span> Thread(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Runnable() {
        @Override
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() {
            System.out.println(</span>"关上冰箱!"<span style="color: rgba(0, 0, 0, 1)">);
        }
    });
    executorService.submit(thread1);
    executorService.submit(thread2);
    executorService.submit(thread3);
    executorService.shutdown();        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用完毕记得关闭线程池</span>

}

}
// 输出结果:
打开冰箱!
拿出一瓶牛奶!
关上冰箱!

复制代码

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值