简体   繁体   English

在Java中使用队列的最佳方法

[英]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. 我想做的是让4个队列运行,每个队列都有一个优先级,因此每个队列都以循环格式运行预定的时间,并考虑为每个队列使用Priority Queue,然后我在Java api中找到了链表也是一个队列(如果我读对了),它是一个FIFO队列。 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. 您始终可以实现Java集合库的LinkedList,并调整一些方法将其转换为优先级队列。 Bam--best of both worlds. Bam-两全其美。

A way to modify it to fit your project would be to overload the add() method to include a priority parameter. 修改它以适合您的项目的一种方法是重载add()方法以包含优先级参数。 Although a queue is traditionally FIFO, the LinkedList has a removeFirst() and a removeLast() function (even a "remove at"-- remove(int index) ). 尽管队列传统上是FIFO,但LinkedList具有removeFirst()removeLast()函数(甚至是“ 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. 我认为这是针对学校项目的,因此这也是展示对OOP继承的一种好方法。

Another method is to just have a 'LinkedList' of a "Process" class that you'll define with an int priority property. 另一种方法是仅具有“ Process”类的“ LinkedList”,您将使用int priority属性对其进行定义。 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. 这样,您可以拥有某种流程管理器,该处理程序将通过查看每个对象的priority属性来操纵流程对象的LinkedList。

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(?). 您有4个队列,分别代表4个不同的优先级。 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. 因此,必须有1个以上的生产者线程根据其优先级将作业放入每个队列中(这确实不清楚,所以我猜这是您的意思),并且有1个以上的生产者线程为每个队列提供服务。有自己优先级的队列。 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). 优先级为1的使用者线程从队列1中拉出,优先级2则从队列2中拉出,依此类推(同样没有真正解释过,但是我在这里进行推断)。 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. 它不过是Java实现它的方式而已排序的列表。 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: 从javadoc来看不是很明显,但是您将创建一个像这样的通用作业,并将其添加到优先级队列中以使其正常工作:

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). 另一方面,LinkedList具有排队的方法,但是将其转换为优先级队列将需要您自己添加使之成为优先级队列的方法(即,按排序顺序添加内容)。

If you have to use the 4 separate queues with different priority threads well that's just unfortunate architecture, but you could do that. 如果您必须很好地使用具有不同优先级线程的4个单独的队列,那只是不幸的体系结构,但是您可以这样做。 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. 如果您必须保留4个列表的方法,则每个方法代表优先级或其他。 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. 然后在其上实现一个简单的take()方法。 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. 比较容易,但是我仍然只使用我所描述的PriorityQueue并放弃整个4队列的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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