Dave's Brain

Browse - Programming Tips - How can I enumerate a LinkedBlockingQueue?

Date: 2012may14
Language: Java

Q.  How can I 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.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.