I'm trying to implement a FIFO observer/observable decoupling queue, but I am not sure how to get a method to wait until a queue is not empty before returning. Here's is my current attempt but I'm sure there must a more elegant solution.
/*
* 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();
}
Why not use a BlockingQueue
- this does exactly what you want.
The BlockingQueue
interface has a take()
method for this purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With