Skip to content

Instantly share code, notes, and snippets.

@ecolban
ecolban / gist:21ac0fd081f3ea167cfe
Created May 7, 2014 23:20
How to iterate over a BlockingQueue.
/**
* A BlockingQueue comes with an iterator() method which is more or less unusable because its hasNext()
* method may return false before the last element has been placed on the queue. To indicate that the last
* element has been put on the queue, we use the same trick used at some airports to indicate that the last
* piece of baggage has been placed on the baggage conveyor belt: A red suitcase marks the end.
*/
public class MyClass implements Iterable<String> {
private BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);