Poison

1114. Print in Order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Foo {

private final CountDownLatch latchA;
private final CountDownLatch latchB;

public Foo() {
this.latchA = new CountDownLatch(1);
this.latchB = new CountDownLatch(1);
}

public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
latchA.countDown();
}

public void second(Runnable printSecond) throws InterruptedException {
latchA.await();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
latchB.countDown();
}

public void third(Runnable printThird) throws InterruptedException {
latchB.await();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}

}
Reference

1114. Print in Order