wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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: 18541
    
    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: 18532
    
  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)
 
jQuery in Action, 2nd edition
 
subject: PriorityQueue and Concurrent Modification
 
Similar Threads
priority queue!
Generics
Priority queue
Priority Queue
question regarding PriorityQueue