I need some help concerning deadlocks. I just do not understand why my code is deadlocking here.
I tried different scenarios.
However I still can’t find why and where is the problem. Normally it should work
and I don’t find where the deadlock is between debuter and termine.
public class Interblocking {
protected object obj = object();
private boolean condition = true;
public synchronized void debuter() {
synchronized(obj) {
while (!condition) {
try {
obj.wait();
} catch (InterruptedExeption ie) {}
}
condition = false;
}
}
public synchronized void terminer() {
synchronized(obj) {
condition = true;
obj.notifyAll();
}
}
}
Edit (new answer)
Method
wait()doesn’t release all locks of current Thread.So when A thread invokes
debuterit releases onlyobjlock but holdsthislock so other threads can’t invoketerminermethod.Here is example:
output