我正在Intelij java中做一个项目,我有一个奇怪的问题。 我正在调试时,通知等待机制运行良好, 另一方面,当我点击“F9”跳过所有断点时,apllication会冻结,因为我认为我的所有线程都处于“等待”模式。 这是管理器,它警告线程他们的名字是代理:
synchronized (freeAgentDequeueLock) {
while (currentMissionToDo != null) {
updateFreeAgents();
while (freeAgents.isEmpty()) {
try {
freeAgentDequeueLock.wait();
} catch (InterruptedException e) { }
updateFreeAgents();
}
freeAgent = freeAgents.poll();
synchronized (freeAgent.getLock()) {
freeAgent.setCurrentMissionToDo(currentMissionToDo);
freeAgent.setSecret(secret);
freeAgent.setAgentToReadyToStartProcess();
freeAgent.getLock().notify();
}
currentMissionToDo = missionsToDo.poll();
}
}
这是代理商醒来:
synchronized (lock) {
while (!isKilled) {
while (!readyToStart) {
try {
lock.wait();
} catch (InterruptedException e) { }
}
counterAwaking++;
System.out.println("agent.Agent id: "+ id + " awakened " + counterAwaking + " times");
startDecryptionProcess();
System.out.println("agent.Agent id: "+ id + " finished the " + counterAwaking + " time, going to wait");
readyToStart = false;
}
}
这是通知经理的功能:
public void startDecryptionProcess() {
some code....
synchronized (freeAgentDequeueLock) {
freeAgentDequeueLock.notify();
}
}