Dave's Brain

Browse - 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.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.