forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint in Order.java
More file actions
53 lines (45 loc) · 1.39 KB
/
Print in Order.java
File metadata and controls
53 lines (45 loc) · 1.39 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Foo {
private int counter = 1;
private String mutex = "";
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
boolean flag = true;
while (flag) {
synchronized(mutex) {
if (counter == 1) {
printFirst.run();
counter++;
flag = false;
}
}
}
}
public void second(Runnable printSecond) throws InterruptedException {
// printSecond.run() outputs "second". Do not change or remove this line.
boolean flag = true;
while (flag) {
synchronized(mutex) {
if (counter == 2) {
printSecond.run();
counter++;
flag = false;
}
}
}
}
public void third(Runnable printThird) throws InterruptedException {
// printThird.run() outputs "third". Do not change or remove this line.
boolean flag = true;
while (flag) {
synchronized(mutex) {
if (counter == 3) {
printThird.run();
counter++;
flag = false;
}
}
}
}
}