BlockingQueue 용량 1입니다.받은 재고의 최종 가격을 저장합니다. 클라이언트가 큐를 폴링 할 때까지 가격은 큐에 남아 있습니다. 그런 다음 getLatestPrice()
이라는 메서드를 사용하여 최신 가격을 반환해야합니다. 내 문제는 클라이언트가 아직 폴링하지 않은 경우 최신 가격이 대기열에 없을 수 있다는 것입니다. 차단 된 스레드에있을 수 있습니다. 차단 된 경우 최신 가격을 반환하는 최선의 해결책은 무엇입니까? 고맙습니다. 내가 큐에 저장된 값이 알고 있지만BlockingQueue에서 차단 된 스레드
private final BlockingQueue<PriceUpdate> latest;
private final long pollTimeout = 2;
private TimeUnit pollTimeUnit = TimeUnit.SECONDS;
public SaveListener(int capacity) {
latest = new ArrayBlockingQueue<PriceUpdate>(capacity, true);
}
public void newPrice(PriceUpdate priceUpdate) {
try {
latest.put(priceUpdate);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public PriceUpdate getNewPrice() {
try {
return latest.poll(pollTimeout, pollTimeUnit); }
catch (InterruptedException e) {
return null;
}
}
getLatestPrice()
통화 그러나 그것은 어떤 값을 반환하지 않습니다 getNewPrice
.
왜 차단 대기열을 사용합니까? 가격을 업데이트하는 스레드를 차단해야합니까? – Basilevs