• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

PriorityQueue doubt

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;

class Demo

{

public static void main( String args[] ) {


PriorityQueue<Integer> p=new PriorityQueue<Integer>();

p.offer(1);
p.offer(1);
p.offer(1);//line 1
System.out.println(p.size());

for(int i :p )
{p.poll();
p.poll();


}

}
}
my questions is even though i am modifying p in the for loop its not giving ConcurrentModificationException why so?


if i change line 2 to p.offer(2) then it raises the exception.

why this inconsistency?what happends if i add duplicate elements in PriorityQueue and which element comes out first.

please help.
[ May 06, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if i change line 2 to p.offer(2) then it raises the exception.



Hi,

I am not able to find line 2, if you are saying line 1 then its not giving any exception after that changes.

Abdul Mohsin
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chandra i could not understand you much.

posting the question again.

import java.util.*;

class Demo

{

public static void main( String args[] ) {


PriorityQueue<Integer> p=new PriorityQueue<Integer>();

p.offer(1);
p.offer(1);
p.offer(1);//line 1
System.out.println(p.size());

for(int i )
{p.poll();
p.poll();


}

}
}
my questions is even though i am modifying p in the for loop its not giving ConcurrentModificationException why so?


if i change line 1 to p.offer(2) then it raises the exception.
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more thing what happends if am adding same element twice,like am adding 1 twice to the Priority Queue,which one gets more priority?
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody solve my doubt.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sharan vasandani:
can anybody solve my doubt.



Hi Sharan,

See the code below:




The only problem comes when you use iterator to iterate through your PriorityQueue or so and at the same time you do modification to that hence causing inconsistency; You see while I used isEmpty() method, no problem as such; Because the hasNext() method of the iterator first verifies the consistency (refer to the PriorityQueue.java file to have this clear) before returning the next object. When it checks second time it finds it inconsistent because you have poll(ed) one item from it.


Thanks,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Let us not get the hashNext() method to be called second time in the loop:



Output:

city
country
state
town
villege

In short: the problem can only be caught when the hashNext() method is called next, after your modification.

See by just modifying your code once, twice or more if you still don't get it. It will help for sure.
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks chandra,now its clear.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic