No, it would not block (demo).
Java's for
-each loop gets the iterator
from the collection, and uses that iterator to traverse the collection. According to the documentation, the iterator of LinkedBlockingQueue<T>
collection reflects the state of the queue at the time the iterator is constructed:
The returned iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException
, and guarantees to traverse elements as they existed upon construction of the iterator.
Since the queue is empty at the time the iterator is constructed, the iterator would not wait for more data to become available, and report that it has no additional elements. Of course, if the collection becomes non-empty after the construction of the iterator, the iterator may return some of the newly inserted items.