-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrintInOrder.java
36 lines (27 loc) · 949 Bytes
/
PrintInOrder.java
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
31
32
33
34
35
36
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
/**
* 1114. Print In Order
* @author LBW
*/
public class PrintInOrder {
private AtomicInteger i = new AtomicInteger(0);
public PrintInOrder() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
i.incrementAndGet();
}
public void second(Runnable printSecond) throws InterruptedException {
// printSecond.run() outputs "second". Do not change or remove this line.
while (i.get() != 1);
printSecond.run();
i.incrementAndGet();
}
public void third(Runnable printThird) throws InterruptedException {
// printThird.run() outputs "third". Do not change or remove this line.
while (i.get() != 2);
printThird.run();
}
}