I tought calling wait() then the thread loses the lock and has to regain it? Is this true? But obviously the Thread doesnt leaves the synchronized block. He is waiting on the same line where wait() was invoked and then moves on and doesnt has to re-enter the synchronized block?
Code Example:
private int value;
private boolean valueSet = false;
public synchronized int get()
throws InterruptedException {
if (!valueSet) { //should be while here
this.wait();
}
valueSet = false;
this.notifyAll();
return value;
}
public synchronized void put(final int value)
throws InterruptedException {
if (valueSet) { //should be while here
this.wait();
}
this.value = value;
valueSet = true;
this.notifyAll();
}
}
I thought a Thread can only be inside the synchronized block if he has the lock?