经典多线程问题(五)-按顺序执行的问题
package com.example.demo; import java.util.concurrent.CountDownLatch; /** * @ClassName Foo * @Description: 1114. 按序打印(多线程) * @Author xtanb * @Date 2019/9/23 * @Version V1.0 **/ public class Foo { private CountDownLatch countDownLatchA; private CountDownLatch countDownLatchB; public Foo() { this.countDownLatchA = new CountDownLatch(1); this.countDownLatchB = new CountDownLatch(1); } public void first(Runnable printFirst) throws InterruptedException { // printFirst.run() outputs "first". Do not change or remove this line. printFirst.run(); countDownLatchA.countDown(); } public void second(Runnable printSecond) throws InterruptedException { countDownLatchA.await(); // printSecond.run() outputs "second". Do not change or remove this line. printSecond.run(); countDownLatchB.countDown(); } public void third(Runnable printThird) throws InterruptedException { countDownLatchB.await(); // printThird.run() outputs "third". Do not change or remove this line. printThird.run(); } }