File tree 1 file changed +12
-12
lines changed 1 file changed +12
-12
lines changed Original file line number Diff line number Diff line change 1
1
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 ;
5
5
6
6
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 );
10
9
}
11
10
12
11
public void hydrogen (Runnable releaseHydrogen ) throws InterruptedException {
12
+
13
+ this .hydrogenSemaphore .acquire ();
13
14
// releaseHydrogen.run() outputs "H". Do not change or remove this line.
14
- this .hydrogen .acquire ();
15
15
releaseHydrogen .run ();
16
- this .oxygen .release ();
16
+ this .oxygenSemaphore .release ();
17
17
}
18
18
19
19
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.
22
23
releaseOxygen .run ();
23
- this .hydrogen .release (2 );
24
- this .mutex .release ();
24
+ this .hydrogenSemaphore .release (2 );
25
25
}
26
26
}
You can’t perform that action at this time.
0 commit comments