wait() 및 notify()/notifyall()과 함께 while 루프를 사용할 수 있습니까?Java 100 % CPU로드없이 끊임없이 모니터 할 수 있습니까?
편집 : waitfornextprice 메서드의 else 조건을 putPrice 메서드에서만 실행하려고합니다.
public synchronized void putPrice(String s,int i) {
if (getPrice(s) != i){
this.Prices.put(s, i);
notify();
}
}
public synchronized int waitForNextPrice(String s) {
int b = null;
if (hasPriceChanged(s)==true){
b = getPrice(s);
}
else{
//Need to block here, until putPrice() is called again
while(hasPriceChanged(s)==false) wait();
//Once putPrice has been called, b = getPrice();
b = getPrice(s);
}
return b;
}
확인 필자가 상단 – Jason