[英]why does this code throw a IllegalMonitorStateException?
請向我解釋為什么我的代碼在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();
}
您只能在該對象的synchronized
塊內調用wait()
。
在synchronized (targetW)
內部,可以調用targetW.wait()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.