- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个优先级队列,它按日期顺序列出 SQL 数据库中的作业负载。然后,我得到了下面最接近的DeadlineJob函数,该函数获取最重要的工作,检查是否有任何其他工作具有相同的日期,然后比较优先级以查看哪个是最重要的工作。然后我就得到了最高职位。
查找原始队列顶部作业:
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
if (nextJob != null) {
System.out.println("Found top EDF job:");
printJob( nextJob );
// what is it's date?
Date highestRankedDate = nextJob.getConvertedDeadlineDate();
// create a temporary queue to work out priorities of jobs with same deadline
JobPriorityQueue schedulerPriorityQueue = new JobPriorityQueue();
// add the top job to priority queue
//schedulerPriorityQueue.addJob(nextJob);
for (JobRequest jr : scheduledJobs) {
// go through scheduled jobs looking for all jobs with same date
if (jr.getConvertedDeadlineDate().equals(highestRankedDate)) {
// same date deadline, soadd to scheduler priority queue
schedulerPriorityQueue.addJob(jr);
System.out.println("Adding following job to priority queue:");
printJob(jr);
}
}
JobRequest highestPriorityJob = schedulerPriorityQueue.poll();
// this is the item at the top of the PRIORTY JOB queue to return
// remove that item from scheduledJobs
scheduledJobs.remove(highestPriorityJob);
return highestPriorityJob;
} else {
return null;
}
}
以下代码将顶部作业处理到队列中:
public void processNextJob() {
/*
* 1. get # of free CPU's still avaialble
* 2. get top most job from priority queue
* 3. run job - put to CPU queue
* 4. develop a CPU queue here
* 5. count cores against freeCPUS and some sort of calculation to sort run times
*/
int freeCPUS = 500;
int availableCPUS = 0;
Queue q = new PriorityQueue();
// while(freeCPUS >= 500)
// {
//
// }
JobRequest nextJob = schedulerPriorityQueue.closestDeadlineJob(freeCPUS); // returns top job from queue
if (nextJob != null) {
System.out.println("Top priority / edf job:");
System.out.print(nextJob.getUserID() + "-->");
System.out.print(nextJob.getStartDate() + "--START-->");
System.out.print(nextJob.getEndDate() + "---END-->");
System.out.print(nextJob.getDeadDate() + "--DROP-->");
System.out.print(nextJob.getDepartment() + "-->");
System.out.print(nextJob.getProjectName() + "-->");
System.out.print(nextJob.getProjectApplication() + "-->");
System.out.print(nextJob.getPriority() + "--PRIORITY-->");
System.out.print(nextJob.getCores() + "-->");
System.out.print(nextJob.getDiskSpace() + "-->");
System.out.println(nextJob.getAnaylsis());
// now got correct job based on earliest deadline / priority
// implement a FIFO queue here / execution stack
// add next job here
} else {
System.out.println("Job = null");
}
}
我需要做的是修复我在将最近的DeadlineJob 中的作业放入队列中的糟糕尝试或适应,然后在达到 500 个核心限制时停止将它们放入队列中。目前我只是陷入了 while true 下面的 for 循环中,而且我认为我所设定的方法在离开循环后甚至不起作用。
有什么想法吗?
编辑
public void processNextJob() {
/*
* 1. get # of free CPU's still avaialble
* 2. get top most job from priority queue
* 3. run job - put to CPU queue
* 4. develop a CPU queue here
* 5. count cores against freeCPUS and some sort of calculation to sort run times
*/
int freeCPUS = 500;
int availableCPUS = 0;
JobRequest nextJob = schedulerPriorityQueue.closestDeadlineJob(freeCPUS); // returns top job from queue
if (nextJob != null) {
System.out.println("Top priority / edf job:");
printJob( nextJob );
// go through scheduled jobs looking for all jobs with same date
if (nextJob.getCores() <= freeCPUS) {
// same date deadline, soadd to scheduler priority queue
schedulerPriorityQueue.addJob(nextJob);
System.out.println("Adding following job to execution queue:");
printJob( nextJob );
// can use this to get the next top job but need to add calculations to printout the next top job aslong as CPU less than 500
// schedulerPriorityQueue.closestDeadlineJob(freeCPUS);
// schedulerPriorityQueue.addJob(nextJob);
} else if (nextJob.getCores() > freeCPUS) {
System.out.println("Queue temporarily full");
}
// now got correct job based on earliest deadline / priority
// implement a FIFO queue here / execution stack
// add next job here
} else {
System.out.println("Job = null");
}
}
我想我需要在上面实现一个循环,并移出 if 语句,表示执行下一个作业,如果低于 500 个,则再次循环并获取另一个作业,然后在满足 500 个核心标准时将其放入某种新队列中停止添加到新队列
最佳答案
我会尽可能使用java.util.concurrent
包中的实用程序。
首先,您可以定义一个带有 Comparator
的 PriorityBlockingQueue
,该比较器按日期然后优先级对作业进行排序,因此日期最早和优先级最高的作业始终位于队列的开始:
PriorityBlockingQueue<JobRequest> q = new PriorityBlockingQueue<Test1.JobRequest>(0, new Comparator<JobRequest>()
{
@Override
public int compare(JobRequest o1, JobRequest o2)
{
int dateComparison = o1.getDate().compareTo(o2.getDate());
if (dateComparison != 0)
return dateComparison;
// assume higher number means higher priority
return o2.getPriority() - o1.getPriority();
}
});
我仍然不确定我是否理解您对核心的要求,但您这里有两个选择。如果您希望同时执行最多 500 个作业,然后拒绝新项目,您可以使用带有 SynchronousQueue
的执行器:
ExecutorService executor = new ThreadPoolExecutor(0 /*core size*/,
500 /*max size*/,
0 /*keep alive*/,
TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
或者,如果您希望同时执行更少的作业,您可以使用 ArrayBlockingQueue
,它在满时会阻塞:
ExecutorService executor = new ThreadPoolExecutor(0 /*core size*/,
5 /*max size*/,
0 /*keep alive*/,
TimeUnit.SECONDS,
new ArrayBlockingQueue(500-5)<Runnable>());
然后从队列中提取作业并执行它们,按照您想要的方式处理被拒绝的执行:
while (!isFinished)
{
JobRequest job = q.take();
try
{
executor.execute(job);
}
catch (RejectedExecutionException e)
{
}
}
但是,如果您只想同时运行 500 个作业并将后续作业排队,只需传入 LinkedBlockingQueue
或使用 Executors
上的实用程序方法之一,例如newFixedThreadPool(int nThreads)
。
关于java 优先级队列到队列的适配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15002763/
我的 JavaScript 代码中有一道数学题。我需要将给定数量的玩家随机分成 2 队,这样每次 - 如果玩家想再次比赛 - 团队都会重新组成,并且在形成所有组合之前他们应该不同。 假设我有 4 个玩
我是一名优秀的程序员,十分优秀!