My code is as follows:
private LinkedBlockingQueue<TrackedOperation> operations = new LinkedBlockingQueue<TrackedOperation>(10000);
Iterator<TrackedOperation> it = operations.iterator();
while (it.hasNext()) {
TrackedOperation op = operations.remove();
...
}
My question is:
Would iterator be always pointing to the head of the queue and is that the expected behavior?
If I am not making sense, then my question is what would be the behavior of iterator? Is my operations queue and iterator consistent in the above code?
Here what I do,
while (!linkedBlockingQueue.isEmpty()) {
linkedBlockingQueue.remove(); //do what you want to with it
}
You can only call it.remove() after you've called it.next(). The element returned by it.next() is the one that will then be removed by the it.remove() operation.
Accepting @assylias answer above
You would generally while(true) E e = queue.take(); with a blocking queue. Then interrupt the thread or send a special item to break the loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.