I have created a new Condition chopstickFree
and in my pickUpChopstick()
method, I am waiting for a lock on it but I can't get access to it at all.
Through debugging I have found that when it gets to the chopstickFree.await()
line in the pickUpChopstick()
method, it just pauses indefinitely
I don't understand? That code in the constructor was just an unsure attempt to get it working but either way, I have created a new condition, signaled to all that it is free, but I can't get a lock on it at all?
public class Chopstick {
Lock lock = new ReentrantLock();
private Condition chopstickFree = lock.newCondition();
private Condition chopstickInUse = lock.newCondition();
Chopstick() {
lock.lock();
chopstickFree.signalAll();
lock.unlock();
}
// Pick up chopstick
public void pickUpChopstick() throws InterruptedException {
lock.lock();
try {
chopstickFree.await(); // ALWAYS PAUSES HERE INDEFINITELY
chopstickInUse.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
// Release chopstick
public void releaseChopstick() {
lock.lock();
chopstickFree.signal();
lock.unlock();
}
}
Any ideas?
Cheers