-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFoo.java
More file actions
50 lines (38 loc) · 1.14 KB
/
Foo.java
File metadata and controls
50 lines (38 loc) · 1.14 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
class Foo {
private Object lock;
private boolean oneDone;
private boolean twoDone;
public Foo() {
lock = new Object();
oneDone = false;
twoDone = false;
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
synchronized (lock){
printFirst.run();
oneDone = true;
lock.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
// printSecond.run() outputs "second". Do not change or remove this line.
synchronized(lock){
while(!oneDone){
lock.wait();
}
printSecond.run();
twoDone = true;
lock.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
// printThird.run() outputs "third". Do not change or remove this line.
synchronized(lock){
while(!twoDone){
lock.wait();
}
printThird.run();
}
}
}