-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintInOrder.java
More file actions
40 lines (31 loc) · 1.07 KB
/
PrintInOrder.java
File metadata and controls
40 lines (31 loc) · 1.07 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
class Foo {
private Semaphore sem1, sem2;
public Foo() {
sem1 = new Semaphore(1);
sem2 = new Semaphore(1);
try {
sem1.acquire();
sem2.acquire();
} catch (InterruptedException e) {
System.out.println("Something went wrong while acquiring semaphores");
}
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
sem1.release();
}
public void second(Runnable printSecond) throws InterruptedException {
sem1.acquire();
sem1.release();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
sem2.release();
}
public void third(Runnable printThird) throws InterruptedException {
sem2.acquire();
sem2.release();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}