[英]why does this code throw a IllegalMonitorStateException?
Please explain to me why my code throws a IllegalMonitorStateException
at the wait function, as far as I know this only happens if its not done in a synchronized part? 请向我解释为什么我的代码在wait函数上抛出
IllegalMonitorStateException
,据我所知这仅在未在同步部分完成的情况下才会发生?
private void deliver(int target) {
Warehouse targetW = targets[target];
targetW.deliver();
System.out.println(name + " starts to deliver too " +
targetW.getName());
int sleepTime = DELIVERY_TIME / LOADING_CAPACITY;
int counter = 0;
while (counter < LOADING_CAPACITY) {
synchronized (targetW) {
while (!targetW.fill(1)) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
counter++;
try {
sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
leaveMSG(targetW);
targetW.delivered();
}
You can only call wait()
inside a synchronized
block on that object . 您只能在该对象的
synchronized
块内调用wait()
。
Inside synchronized (targetW)
, you can call targetW.wait()
. 在
synchronized (targetW)
内部,可以调用targetW.wait()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.