This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " PriorityQueue  and Concurrent Modification" Watch " PriorityQueue  and Concurrent Modification" New topic

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

Joined: Oct 14, 2005
Posts: 19850

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

Joined: Sep 28, 2004
Posts: 20370

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
wood burning stoves
subject: PriorityQueue and Concurrent Modification
It's not a secret anymore!