1

Is the element currently being processed in a for loop, the head of the queue?

private Queue<User> users = new ConcurrentLinkedQueue<User>();

for(User u : users){
    users.remove(); // <- is this removing the currently iterated element?
}

Or is using users.remove(u) preferable here?

ThatGuy343
  • 2,354
  • 3
  • 30
  • 55
  • The [javadoc of remove()](http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#remove%28%29) says: *Retrieves and removes the head of this queue*. What isn't clear? – JB Nizet Jun 21 '15 at 09:15
  • I am using more than one for loop in separate threads and was wondering if the head of the queue was either the next element or the element being iterated. – ThatGuy343 Jun 21 '15 at 09:17
  • 1
    The head of the queue is the head of the queue. Just like, when people form a queue to wait for the bus, the first person is the head of the queue. Whether you're iterating on the queue or not doesn't change the definition of "head". – JB Nizet Jun 21 '15 at 09:19
  • What happens if another thread removes an element while your loop is executing? Then the "currently iterating element" and "head of queue" might get out of sync. – user253751 Jun 21 '15 at 09:24
  • What If i were to use a Java 8 streamForEach? Would the head still be the element iterated? Or would the parallelism mess that up. – ThatGuy343 Jun 21 '15 at 09:25
  • @immibis that's why im using a ConcurrentLinkedQueue, it supports multiple threads removing/adding to it from what i've read – ThatGuy343 Jun 21 '15 at 09:26
  • 1
    @ThatGuy343 Indeed. But what do you think happens if your for-each loop starts and gets the first element, and then the other thread removes the first element, and then your loop removes the *second* element? Then you've removed the wrong element! – user253751 Jun 21 '15 at 09:29
  • @ThatGuy343 I suppose it depends: what is this loop supposed to do? – user253751 Jun 21 '15 at 09:30
  • 1
    It is thread safe in a way that poll() and offer() methods are atomic. You should make use of those in multi threaded environment. – John Jun 21 '15 at 09:30
  • \Well then a queue is probably not the best thing to use for this... what would you suggest for a concurrent set? @user3360241 where can i see which methods are atomic? – ThatGuy343 Jun 21 '15 at 09:32
  • @ThatGuy343 The Javadoc for ConcurrentLinkedQueue says which methods aren't atomic (not many), the rest are. – user253751 Jun 21 '15 at 09:33
  • 1
    Docs also state this: Iterators are weakly consistent, returning elements reflecting the state of the queue at some point at or since the creation of the iterator. Link to answer explaining weakly consistent: http://stackoverflow.com/questions/20142493/fail-safe-iterators-and-weakly-consistent-iterators – John Jun 21 '15 at 09:35

1 Answers1

3

Yes, that is correct for ConcurrentLinkedQueue<E> since it orders elements in FIFO order.

From the docs:

This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time.

Tiny
  • 27,221
  • 105
  • 339
  • 599
John
  • 5,189
  • 2
  • 38
  • 62
  • 1
    This does **NOT** mean the call to `remove` will always remove the element being iterated over. – user253751 Jun 21 '15 at 12:18
  • Care to elaborate comment? In the answer it is stated that iteration is done with respect to insertion order as this collection maintains FIFO ordering. Also, quote from the docs regarding the remove: public E remove() Retrieves and removes the head of this queue. This method differs from poll only in that it throws an exception if this queue is empty. This implementation returns the result of poll unless the queue is empty. – John Jun 21 '15 at 12:33
  • As I said in a comment on the question: If another thread calls `remove` while this loop is running, then the iterator will get out of sync with the head of the queue. – user253751 Jun 22 '15 at 02:48
  • Yes that is true but that premise was not in the question... To be honest you could make majority of answers invalid by adding threading issues. – John Jun 22 '15 at 08:20
  • Besides, if you look at my comments bellow your comments you will notice that i mention iteration is weakly consistent. – John Jun 22 '15 at 08:24