• Post Reply Bookmark Topic Watch Topic
  • New Topic

Priority Queue printing to see if elements are even in the queue  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to learn about queues and am a bit stuck and dont really know if this is working or not.
What I want to do is to add a object of Processes labled p1 p2 p3 ect.... which has the characteristics like this
public int priority; //not to exceed 4
public int arrivalTime;
public int finishTime;

this is one class with some get and set methods and what not they all work fine.

This is my comparator that I made


And this looks like it should work well but not 100% on this I just following some stuff I found online for this.

and here is where i put them in the queue and print them off




I dont know if this is working becuase it gives me a
"Exception in thread "main" java.util.ConcurrentModificationException"
when it hits the foreach loop when I am trying to print this off.

also how can i print off something more meaningful than Process@1fb8ee3 I really dont know how to get useful information out of the queue.
Thank you for any help with this.
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for-each loop which you used there uses an Iterator under the covers to go through the queue one Process at a time. And you're removing the processes from the queue inside that loop, which the Iterator doesn't permit.

However the solution should be simple: since the method is only supposed to print the Processes in the queue, it shouldn't be removing them from the queue anyway. So don't do that.

As for your other question, if you provide your Process class with a method like this:



Then you'll see the result of that method when you pass a Process object to System.out.println.

 
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Millar wrote:I dont know if this is working becuase it gives me a
"Exception in thread "main" java.util.ConcurrentModificationException"
when it hits the foreach loop when I am trying to print this off.

The problem is that while you are looping through the queue with the foreach, you're also calling remove(). (What method are you really calling, anyway? Perhaps remove(Object)?)

Basically you can't modify the queue while looping through it - unless you use the Iterator to do the modification. Which is impossible with foreach; you would need to use an old-fashioned while or for loop on the Iterator directly, and call remove() on the Iterator object. But what's the point? Why are you removing anything at all in a method that supposedly just prints what's in the queue? Why not just print the values in the queue?

Matt Millar wrote:also how can i print off something more meaningful than Process@1fb8ee3 I really dont know how to get useful information out of the queue.
Thank you for any help with this.

You probably need to override the toString() method in the Process class.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are a couple additional notes:

Matt Millar wrote:
public int priority; //not to exceed 4


  • Member variables should be private.
  • You have this priority field, but you're not using it to determine the priority in your queue. Is that an oversight or is there a reason for that?






  • There's no need to cast a an b to Process, as they're already declared to be of that type.
  • There's already a class in the core API called Process. It's generally a bad idea to name your own classes the same as those in the core API. It can lead to confusion and unexpected behavior.
  •  
    Marshal
    Posts: 56600
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And welcome to the Ranch
     
    Matt Millar
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First of all thank you for all of your help everybody I made a lot of improvements to my code just last night thanks to everyone!
    But now when i go about printing the queue i either only print 5 of them or just 10 of the same one.
    In the MyProcess Class i added a print method which works great


    and here is where I call the print method in the main class


    I tried peek which gives me the same copy of the first object in the queue and that seams right order so far as I know.
    I also tried the remove() method and that just gave me the first 5 in the queue and then stopped printing or something.
     
    Mike Simmons
    Ranch Hand
    Posts: 3090
    14
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Earlier you were using a foreach loop, which seemed really close to the solution. Why not just use a foreach loop, and don't use peek() or remove() at all? Just print each element in the foreach loop.
     
    Matt Millar
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ahhh thank i got it all working thank to everyone I went back to the for each to print like this and it works just fine


     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Matt Millar wrote:Ahhh thank i got it all working thank to everyone I went back to the for each to print like this and it works just fine




    One caveat there, from PQ's javadocs: "The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray())."

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!