aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  PriorityQueue  and Concurrent Modification Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 13842

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: 14606



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)
 
 
subject: PriorityQueue and Concurrent Modification
 
Threads others viewed
Priority queue
Generics
priority queue!
Priority Queue
question regarding PriorityQueue
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture