Java中PriorityQueue类的添加操作

时间:2012-10-14 16:42:20

标签: java eclipse runtime-error priority-queue user-defined-types

我正在尝试在Java中实现PriorityQueue。我有PrinterQueue类,包括用于测试的PriorityQueue和TestPrinterQueue类。 PrintJob是此队列中的类型。构造函数是私有的,所以我使用反射来实现对象的实例。

运行时出现问题,我无法向队列添加多个对象。队列的大小始终相同。没有编译错误。所有代码如下。谢谢......

PrinterQueue class:

import java.util.Comparator;
import java.util.PriorityQueue;

public class PrinterQueue{
   private static PrinterQueue queue;
   PriorityQueue<PrintJob> pqExample;
   Comparator<PrintJob> Comparator;
   int iQueueSize = 50;

   private PrinterQueue(){
      Comparator = new Priority();
      pqExample = new PriorityQueue<PrintJob>(iQueueSize,Comparator);
  }
   public static PrinterQueue getQueue(){
      if (queue==null) queue = new PrinterQueue();
     return queue;} 

   public void addJob (PrintJob job) {
       queue = new PrinterQueue();
       queue.pqExample.add(job);
     // Use the job’s getPriority method to check its priority. PRIORITY CLASSINDA KULLANILDI.
     System.out.println("Job " + job.getName() + " is added to the printer queue" + " Size: " + queue.pqExample.size());
     Print();
  }
  public boolean isEmpty(){
      if(queue.pqExample.isEmpty())
          return true;
      else
          return false;
  }
  public PrintJob removeJob(){
      //TODO –remove the highest priority job from the queue
      PrintJob job = queue.pqExample.remove();
      System.out.println("Job "+ job.getName()+ "has been printed");
      return job;

  }
  public boolean isFull(){
      if (queue.pqExample.size() == iQueueSize)
          return true;
      else
          return false;
  }
  private void Print() {

      PriorityQueue<PrintJob> pqTemp;
      pqTemp = queue.pqExample;

       while (pqTemp.size() != 0)
       {
           System.out.println(queue.pqExample.element());
           System.out.println("//");
           pqTemp.remove();
       }
  }
}

TestPrinterQueue类:

import java.lang.reflect.Constructor;
import java.util.Scanner;

public class TestPrinterQueue {

    public static void main (String[] args) throws Exception {

        Constructor<PrinterQueue> constructor = PrinterQueue.class.getDeclaredConstructor(new Class[0]);
        constructor.setAccessible(true);

        PrinterQueue pqTest = constructor.newInstance(new Object[0]);

        @SuppressWarnings("resource")
        Scanner scUserInput = new Scanner(System.in);

        while (true)
        {
             PrintJob pjWillbeAdded = new PrintJob();

             System.out.println("Enter the priority:");
             pjWillbeAdded.setPriority(scUserInput.nextInt());
             System.out.println("Enter the JobName:");
             pjWillbeAdded.setName(scUserInput.next());
             System.out.println("Enter the Number of Pages in PrintJob:");
             pjWillbeAdded.setiNoPJ(scUserInput.nextInt());

             pqTest.addJob(pjWillbeAdded);

             System.out.println("**** TEST SIZE : " + pqTest.pqExample.size() + "*****");
        }
    }
}

我希望这些信息足以解释我的问题。

1 个答案:

答案 0 :(得分:1)

在addJob()方法中,每次都要将队列分配给新的PrinterQueue对象。将其更改为“queue = getQueue()”应该可以解决这个问题。