Consider the following snippet. qIn is a BlockingQueue, LinkedBlockingQueue implementation
while (true) {
try {
// retrieved = qIn.poll(999, TimeUnit.MILLISECONDS); works
retrieved = qIn.poll(); // fails
if (retrieved != null)
consume(retrieved);
if (!Producer.isAddingData && qIn.size() == 0)
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Let me clarify what i mean by works and fails. When i say qIn.poll() fails, i refer to the fact that sometimes not all data in queue is processed.
Why it is the wait (qIn.poll(999, TimeUnit.MILLISECONDS) necessary
retrieved = qIn.poll(999, TimeUnit.MILLISECONDS); // works
retrieved = qIn.poll(); // fails
When the following check is always being performed? Basically the check is “If your producer is no longer adding data and the queue to pull from contains no data, break“
if (!StopwordsFilter.isAddingData && qIn.size() == 0)
break;
I wonder, under what circumstance qIn.poll(); fails?
Defined as follows, considering we break only when there is nothing to take, why would it break?
retrieves and removes the head of this queue, or returns null if this
queue is empty.
PS: I should add that qIn.poll() fails sometimes, not all the time
poll()can returnnullbecause the information hasn’t been added to the queue quite yet, that’s why — but perhaps it might be added in a couple more milliseconds. poll() doesn’t do any waiting at all.If you want to wait…then you’re going to need to guess an upper bound on the time to wait. There’s no really good way of predicting this, but you can probably be generous — if I understand the situation correctly, you’re usually adding things to the queue quite quickly.