Skip to content

Commit b66408c

Browse files
authored
Update Building H2O.java
1 parent 95da6a2 commit b66408c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Concurrency/Building H2O.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
class H2O {
2-
private Semaphore hydrogen;
3-
private Semaphore oxygen;
4-
private Semaphore mutex;
2+
3+
private final Semaphore hydrogenSemaphore;
4+
private final Semaphore oxygenSemaphore;
55

66
public H2O() {
7-
this.hydrogen = new Semaphore(2);
8-
this.oxygen = new Semaphore(0);
9-
this.mutex = new Semaphore(1);
7+
this.hydrogenSemaphore = new Semaphore(2);
8+
this.oxygenSemaphore = new Semaphore(0);
109
}
1110

1211
public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
12+
13+
this.hydrogenSemaphore.acquire();
1314
// releaseHydrogen.run() outputs "H". Do not change or remove this line.
14-
this.hydrogen.acquire();
1515
releaseHydrogen.run();
16-
this.oxygen.release();
16+
this.oxygenSemaphore.release();
1717
}
1818

1919
public void oxygen(Runnable releaseOxygen) throws InterruptedException {
20-
this.mutex.acquire();
21-
this.oxygen.acquire(2);
20+
21+
this.oxygenSemaphore.acquire(2);
22+
// releaseOxygen.run() outputs "O". Do not change or remove this line.
2223
releaseOxygen.run();
23-
this.hydrogen.release(2);
24-
this.mutex.release();
24+
this.hydrogenSemaphore.release(2);
2525
}
2626
}

0 commit comments

Comments
 (0)