我正在尝试模拟电梯多线程移动。
在一楼,几名乘客进入电梯,其他乘客进入睡眠状态(condition.await())。如何从Controller run()方法唤醒/发信号通知二楼的所有其他乘客线程(他们正在等待,因为他们没有进入一楼)?
我为每位乘客提供了run()方法:
@Override
public void run () {
lock.lock();
try {
while (!iCanEnter(passenger)) {
condition.await();
}
passenger.setState(TransportationState.IN_PROGRESS);
leaveDispatchEnterElevator(passenger);
while (!iCanLeave(passenger)) {
condition.await();
}
passenger.setState(TransportationState.COMPLETED);
leaveElevatorEnterArrival(passenger);
} catch (InterruptedException e) {
throw new RuntimeException("Thread has been interrupted..." + e);
} finally {
lock.unlock();
}
}
带电梯移动的控制器run()方法:
@Override
public void run () {
try {
while (!isFinished()) {
if(weCanMove()) {
moveElevator();
}
}
} catch (InterruptedException e) {
throw new RuntimeException("Thread has been interrupted..." + e);
}
}