我在signal / await之前打印出hashcode,它们是相同的,所以它们使用的条件应该相同。
需要锁定/条件,因此我无法使用其他同步技术......
火车:
currStation.getLock().lock();
try{
System.out.println(currStation.getTrainCond().hashCode());
while(seats > 0 && currStation.getWaitingPassengers() > 0){
currStation.getTrainCond().signal();
//thread doesn't end.
try {
thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
finally{
currStation.getLock().unlock();
}
乘客:
station.getLock().lock();
try {
System.out.println(station.getTrainCond().hashCode());
station.getTrainCond().await();
while(!station.getSeat(this)){
//keep running
try {
thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
station.getLock().unlock();
}
}
站& currStation是同一个对象;谢谢:)!