forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1114.java
44 lines (35 loc) · 1.33 KB
/
_1114.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
37
38
39
40
41
42
43
44
package com.fishercoder.solutions;
public class _1114 {
public static class Solution1 {
static class Foo {
private static volatile boolean onePrinted;
private static volatile boolean twoPrinted;
public Foo() {
onePrinted = false;
twoPrinted = false;
}
public synchronized void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
onePrinted = true;
notifyAll();
}
public synchronized void second(Runnable printSecond) throws InterruptedException {
while (!onePrinted) {
wait();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
twoPrinted = true;
notifyAll();
}
public synchronized void third(Runnable printThird) throws InterruptedException {
while (!twoPrinted) {
wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
}
}