Programming Tips - How can I drain a LinkedBlockingQueue?

Date: 2012may14 Language: Java Q. How can I drain a LinkedBlockingQueue? A. Use lots of remove()'s, like this:
LinkedBlockingQueue<Stuff> MyQueue = new LinkedBlockingQueue<Stuff>(); Stuff stuff; for (;;) { try { stuff = MyQueue.remove(); } catch (NoSuchElementException ex) { break; } // Do things }
I like this (vs. other ways) because it does what it says. It keeps removing until there is no more.