I have a homework assignment called “The H2O problem” where I’m supposed to implement a class called H2OBarrier which has 3 methods.
- HReady, a method called when a hydrogen atom (thread) is ready
- OReady, a method called when an oxygen atom (thread) is ready
- makeWater, a method called when 2 hydrogen atoms and one oxygen atom are ready
I’m supposed to do this using Java Reentrant locks and Conditions.
This is my code so far, and I’m wondering if I’m properly using lock and unlock.
public class H2OBarrier {
int hCount;
int oCount;
Lock lock = new ReentrantLock();
Condition hWait = lock.newCondition();
Condition oWait = lock.newCondition();
public void HReady() {
lock.lock();
hCount++;
try {
hWait.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void OReady(){
lock.lock();
oCount++;
try {
while(hCount<2 && oCount<1){
oWait.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
makeWater();
}
}
public void makeWater(){
hCount--;
hWait.signal();
lock.unlock();
hCount--;
hWait.signal();
lock.unlock();
oCount--;
oWait.signal();
lock.unlock();
}
}
Should I be calling unlock() anywhere other than in my makeWater method? The whole flow of the program seems to make pretty logical sense to me, I’d just like to make sure what I’m doing looks correct overall.
Your code is producing a deadlock. Imagine 5 o atoms go through first, 5 go into o queue produced by await(). Now it doesn’t matter if 2 h atoms go through, all the h atoms will automatically wait because of your code.