posted 18 years ago
Hi Sharan,
As I understood your question:
"If you comment line 1, there is no ConcurrentModificationException".
Right?
OK, now its answer
PriorityQueue has two elements, you enter into the loop, poll one item,
in the next iteration PQ looks for the second item that doesn't exist (because you have removed that), so comes out of loop; See the size of the PQ outside the loop, you will see it 1.
In case of three items, enter into the loop, poll the item, next iteratation
PQ is in inconsistent state hence ConcurrentModificationException state.
So don't modify the PQ inside that loop, otherwise you will get such an
unexpected output.
For actual detail, see the PriorityQueue.java soruce file specially
form Line 411 to 429.
If I have to summarize: before next() of the iterator returns it calls
checkForComodification(); method which checks whether there is any mismatch
in size of PQ was and now it is.
[ May 03, 2007: Message edited by: Chandra Bhatt ]