简体   繁体   中英

Best way to use a queue in java

I was working on a project and ran into some issues which i dont know what would be best. What I want to do is have 4 queue running each queue has a priority to it so each one runs for its predetermined amount of time in a round robin format and was thinking of using the Priority Queue for each queue then I found in the Java api that a linked list is also a queue (if im reading that right) which is a FIFO queue. The main goal of the project is to take a "process" and then assign it a priority and let this process run in one of the queue then re-assets its priority and either change the queue its in or leave it where it is.
Which would be a better approach the linked list or the priority queue? Thanks for any input on this

You can always implement the Java Collection Library's LinkedList and tweak some methods to turn it into a priority queue. Bam--best of both worlds.

A way to modify it to fit your project would be to overload the add() method to include a priority parameter. Although a queue is traditionally FIFO, the LinkedList has a removeFirst() and a removeLast() function (even a "remove at"-- remove(int index) ). So, you have all the tools you need with the library class. I assume this is for a school project, so it's a nice way to show off an understanding of OOP inheritance also.

Another method is to just have a 'LinkedList' of a "Process" class that you'll define with an int priority property. That way, you can have some sort of process manager that will manipulate the LinkedList of Process objects by peering through each object's priority property.

Your question had a lot of grammar errors that made it hard to read so I don't know if I fully understand your question, but let's try.

You have 4 queues that represent 4 different priority levels(?). So there must be 1+ producer threads that are dropping jobs into each of the queues based on their priority (this was really unclear so I'm guessing this is what you meant), and there are 1+ consumer threads that service each of the queues that have their own priority. A consumer thread with priority 1 pulls from queue 1, priority 2 pulls from queue 2, etc (again not really explained, but I'm inferring here). And you want to know if these queues should be priority queue or a linked list.

Well if you use a priority queue the queue sorts itself by the priority of the job added to it. It's nothing more than a sorted list is the way Java implements it. So higher priority jobs will move up in the queue ahead of lower priority jobs. By using a priority queue there's no need to have multiple queues or even threads with priorities. You just use a priority queue and it will add the jobs in the order of their priority. Jobs with higher priority will be pushed ahead and done before jobs of lower priority.

It's not obvious from the javadoc but you'd create a generic job like this and add it to the priority queue to get it to work:

public class Job<T> implements Comparable<Job<T>> {

   public enum Priority { LOW, MEDIUM, HIGH, HIGHEST };

   private T payload;

   private Priority priority;

   public Job( T payload, Priority priority ) { .... }

   public T getPayload() { return payload; }

   public Priority getPriority() { return priority; }

   public int compareTo( Job<T> that ) {
      if( this.priority.ordinal() < that.priority.ordinal() ) {
         return 1;
      } else if( this.priority.ordinal() > that.priority.ordinal() ) {
         return -1;
      } else {
         return 0;
      }
   }
}

Done. No fancy priorities on threads, switching queues, or what not. It's one queue, many consumers, just easy grab the next thing and go. Much simpler architecture that probably would work better (ie more through put) than what you described.

A LinkedList, on the other hand, has methods for queueing, but turning it into a priority queue would require you add the methods yourself for making it a priority queue (ie adding things in sorted order).

If you have to use the 4 separate queues with different priority threads well that's just unfortunate architecture, but you could do that. I think in that case the difference between a priority queue and a linked list is zero. Because you'd never have anything in the priority queue of differing priorities. That's just a linked list. If you have to keep the 4 lists approach where each one represents priority or whatever. I would write a simple class that owns the four lists, and the consumer threads would have a reference to it. Then implement a simple take() method on it. And it would pull the job of the highest priority and return it to that thread. That way your threads don't have to know about priority and can really pull from any queue. Easier, but I still would just use a PriorityQueue as I described and ditch the whole 4 queue idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM