mariolpantunes :
I have already searched this error in here, but I think that my piece of code looks correct:
- I gain the lock outside the try..finally
- I have an unlock in the finally section
- I only tried to wait on the condition inside the lock.
- I even print if the current lock is held by this thread and it returns true.
This is an excerpt of the code, if I tried to run the code I get a java.lang.IllegalMonitorStateException: current thread is not owner. The error is in the cond.wait() method.
public void takeARest() {
lock.lock();
try {
while (disembark < totalPassengers) {
System.err.printf("Held by %s%n",lock.isHeldByCurrentThread());
cond.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
Any ideas?
k5_ :
For that you want Condition.await()
.
Object.wait()
is a different method that requires to hold the monitor of the object (synchornized(cond){}
around the call)
So:
public void takeARest() {
lock.lock();
try {
while (disembark < totalPassengers) {
System.err.printf("Held by %s%n",lock.isHeldByCurrentThread());
cond.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=356081&siteId=1