-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
39 lines (35 loc) · 1.03 KB
/
Solution.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
class ZeroEvenOdd {
private int n;
private Semaphore z = new Semaphore(1);
private Semaphore e = new Semaphore(0);
private Semaphore o = new Semaphore(0);
public ZeroEvenOdd(int n) {
this.n = n;
}
// printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for (int i = 0; i < n; ++i) {
z.acquire(1);
printNumber.accept(0);
if (i % 2 == 0) {
o.release(1);
} else {
e.release(1);
}
}
}
public void even(IntConsumer printNumber) throws InterruptedException {
for (int i = 2; i <= n; i += 2) {
e.acquire(1);
printNumber.accept(i);
z.release(1);
}
}
public void odd(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; i += 2) {
o.acquire(1);
printNumber.accept(i);
z.release(1);
}
}
}