Programming Tips - Java: Best way to enumerate a LinkedBlockingQueue

Date: 2012may14 Language: Java Q. Java: Best way to enumerate a LinkedBlockingQueue A. Like this:
LinkedBlockingQueue<Stuff> myQueue = new LinkedBlockingQueue<Stuff>(); for (Iterator<MyStuff> it = myQueue.iterator(); it.hasNext(); ) { Stuff stuff = it.next(); if (stuff == null) break; // Redundant // Do things with stuff }
What if the queue is changed during your loop? That seems to be taken care of my the LinkedBlockingQueue. Or you could make a lock to protect the entire loop.