1

Java優先キューを反復処理してキューの日付の先頭に読み込むwhileループを実行しようとしています。1つは日付値を持ち、キューの残りの部分を調べて、この日付が他の場所で使用されているかどうかを確認します。使用されている場合は、これらの要素を一時的に独自のキューに追加して、別のコンパレータメソッドを呼び出して整理できるようにします。

public JobRequest closestDeadlineJob(int freeCPUS) {
    // find top job to determine if other jobs for date need to be considered
    JobRequest nextJob = scheduledJobs.peek(); // return top most job
    // what is it's date?
    Date currentDate = nextJob.getConvertedDeadlineDate();
    JobPriorityQueue schedulerPriorityQueue = new JobPriorityQueue();
    schedulerPriorityQueue.addJob( nextJob );

    while(true) {


    }

    // this is the item at the top of the PRIORTY JOB queue to return 
    // remove that item from scheduledJobs
   // return null; // replace with to the one you want to return
}

あなたがあまり見ることができないように、私がこれまでに持っているもの

4

2 に答える 2

3

PriorityQueue does not provide you a sorted iteration order. The only guarantee of a PriorityQueue is that extraction methods (peek/poll/remove) will return a smallest element in the set according to your Comparator. If you need a sorted iteration order - use TreeSet/TreeMap instead.

于 2013-03-04T01:35:54.017 に答える
1
import java.util.Date;
import java.util.Comparator;
import java.util.PriorityQueue;

class Job implements Runnable{

 Priority priority;
 Date dateOccurance;

 public Job(Priority priority, Date occurance){
    this.priority = priority;
    this.dateOccurance = occurance;
 }

 public void run(){
    //Job execution
    System.out.println("executed");
 }
}

enum Priority {
 High,
 Medium,
 Low
}

class JobComparator implements Comparator<Job>  {

 @Override
 public int compare(Job j1, Job j2) {

    if(j1.priority.ordinal() > j2.priority.ordinal()) {
        return 1;
    } else if (j1.priority == j2.priority) {
        if(j1.dateOccurance.after(j2.dateOccurance)) {
            return 1;
        } else if (j1.dateOccurance.before(j2.dateOccurance)) {
            return -1;
        } else {
            return 0;
        }
    }  
    return -1;
 }

}

public class PriorityQueueTest {

public static void main(String[] args) throws InterruptedException {
    Date d = new Date();
    Job job1 = new Job(Priority.High, d);
    Job job2 = new Job(Priority.High, d);
    Job job3 = new Job(Priority.Medium, d);
    Job job4 = new Job(Priority.Low, d);    
    Thread.sleep(2000);
    Date l = new Date();
    Job job5 = new Job(Priority.Low, l);    

    Comparator<Job> jComp = new JobComparator();
    PriorityQueue<Job> queue = 
        new PriorityQueue<Job>(10, jComp);

    queue.add(job4);
    queue.add(job3);
    queue.add(job1);
    queue.add(job2);
    queue.add(job5);


    while (queue.size() != 0)
    {
        Job j = queue.remove();
        System.out.println(j.priority +"     "+j.dateOccurance);
    }
 }      

}
于 2013-02-17T08:55:18.920 に答える