This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  PriorityQueue  and Concurrent Modification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " PriorityQueue  and Concurrent Modification" Watch " PriorityQueue  and Concurrent Modification" New topic
Author

PriorityQueue and Concurrent Modification

Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

Should not the following code give ConcurrentModificationException as we are
using the for-each loop and doing poll() on the PriorityQueue





The output is =============== 2.

Could not understand why this is happening ??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19115
    
    8

I don't see anything in the API documentation for the iterator() method of PriorityQueue which suggests it might do that.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19235
    
  40



Agreed. Modifying a collection while iterating is not a good idea because you can get a concurrent modification exception. It doesn't mean that you always will.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PriorityQueue and Concurrent Modification