我正在尝试实现一个 FIFO 观察器/可观察解耦队列,但我不确定如何获得一种方法来等待队列不为空后再返回。这是我目前的尝试,但我相信一定有更优雅的解决方案。
/*
* Waits until there is data, then returns it.
*/
private Double[] get() {
while (queue.isEmpty()) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// Don't care.
}
}
return queue.removeFirst();
}
最佳答案
为什么不使用 BlockingQueue
- 这正是您想要的。
关于java - 如何等待队列包含元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10258604/