短作业java,java短作业优先算法

单通短作业优先算法,求其平均周转时间。

场景:

4个任务同时到达(J1(1),J2(4),J3(10),J4(7),括号中的值是需要的执行时间),每次只能执行一个任务,那么短作业优先算法按任务照执行时间的大小,优先执行时间短的任务,也就说执行顺序是J1,J2,J4,J3。执行的过程如下所示

执行顺序

执行时间

周转时间(等待时间 + 执行时间)

J1

1

1

J2

4

5

J4

7

12

J3

10

22

10(平均周转时间)

java实现上述算法:

package com.sirding.testalgo;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import org.junit.Before;

import org.junit.Test;

/**

* @Described: Spf = Shortest Process First.最短优先算法

* @project: com.sirding.testalgo.Spn

* @author :

* @date :

*/

public class Spf {

private List jobList = new ArrayList();

private long[] arr = {1, 4, 10, 7};

/**

* @Described: 单通道多任务不同时到达

* @author:

* @date :

*/

//@Before

public void diffTime(){

long start = System.currentTimeMillis();

for(long tmp : arr){

jobList.add(new SpfJob(start + (int)(Math.random()*100), tmp));

}

sortList();

}

/**

* @Described: 单通道多任务同时到达

* @author:

* @date :

*/

@Before

public void sameTime(){

long start = System.currentTimeMillis();

for(long tmp : arr){

jobList.add(new SpfJob(start, tmp));

}

sortList();

}

private void sortList() {

Collections.sort(jobList, new Comparator() {

@Override

public int compare(SpfJob o1, SpfJob o2) {

return (int)(o1.execTime - o2.execTime);

}

});

}

/**

* @Described: 单通道最短优先算法-计算平均周转时间

* 测试用例:

* 多个任务同时到达,sameTime()初始化数据

* @author: zc.ding

* @date :

*/

@Test

public void test(){

//方式一:逐步计算每个任务的周转时间

long total = 0;

long costTime = 0;

for(SpfJob job : jobList){

costTime = (costTime + job.getExecTime());

total += costTime;

System.out.println("job_" + job.execTime + "周转时间:" + costTime);

}

//方法二:通过阶乘的方式计算

//for(int i = 0; i < jobList.size(); i++){

//total += (jobList.size() - i) * jobList.get(i).getExecTime();

//}

System.out.println("总周转时间:" + total);

System.out.println("平均周转时间:" + ((double)total/(double)jobList.size()));

}

/**

* @Described: 任务信息

* @project: com.sirding.testalgo.SpfJob

* @author : zc.ding

* @date :

*/

static class SpfJob implements Serializable{

/**

*

*/

private static final long serialVersionUID = 1L;

//到达时间

private long arrivalTime;

//执行时间

private long execTime;

//任务开始时间

private long startTime;

//任务结束时间

private long endTime;

//周转时间

private long totalTime;

public SpfJob(){}

public SpfJob(long startTime, long execTime){

this.startTime = startTime;

this.execTime = execTime;

}

public long getArrivalTime() {

return arrivalTime;

}

public void setArrivalTime(long arrivalTime) {

this.arrivalTime = arrivalTime;

}

public long getExecTime() {

return execTime;

}

public void setExecTime(long execTime) {

this.execTime = execTime;

}

public long getStartTime() {

return startTime;

}

public void setStartTime(long startTime) {

this.startTime = startTime;

}

public long getEndTime() {

return endTime;

}

public void setEndTime(long endTime) {

this.endTime = endTime;

}

public long getTotalTime() {

return totalTime;

}

public void setTotalTime(long totalTime) {

this.totalTime = totalTime;

}

}

}

上述提供了两种实现方式,一种以常规累加的方式进行计算,一种以类似阶乘的方式进行计算。

场景二:

多任务不同时到达,那么要采用的要是应该是FCFS(先来先服务)算法。其周转时间计算过程同场景一中常规计算方式一致。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下 Java 代码实现作业优先算法: ```java import java.util.*; public class ShortestJobFirst { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n, total_time = 0, avg_time; System.out.println("Enter number of processes:"); n = sc.nextInt(); int pid[] = new int[n]; int arrival_time[] = new int[n]; int burst_time[] = new int[n]; int waiting_time[] = new int[n]; int turnaround_time[] = new int[n]; int completion_time[] = new int[n]; float priority[] = new float[n]; for (int i = 0; i < n; i++) { System.out.println("Enter process " + (i + 1) + " arrival time:"); arrival_time[i] = sc.nextInt(); System.out.println("Enter process " + (i + 1) + " burst time:"); burst_time[i] = sc.nextInt(); pid[i] = i + 1; priority[i] = 1; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (burst_time[j] > burst_time[j + 1]) { int temp = burst_time[j]; burst_time[j] = burst_time[j + 1]; burst_time[j + 1] = temp; temp = arrival_time[j]; arrival_time[j] = arrival_time[j + 1]; arrival_time[j + 1] = temp; temp = pid[j]; pid[j] = pid[j + 1]; pid[j + 1] = temp; } } } for (int i = 0; i < n; i++) { waiting_time[i] = total_time - arrival_time[i]; total_time += burst_time[i]; turnaround_time[i] = total_time - arrival_time[i]; completion_time[i] = total_time; avg_time += waiting_time[i]; } System.out.println("PID Arrival time Burst time Waiting time Turnaround time Completion time"); for (int i = 0; i < n; i++) { System.out.println(pid[i] + "\t\t" + arrival_time[i] + "\t\t" + burst_time[i] + "\t\t" + waiting_time[i] + "\t\t" + turnaround_time[i] + "\t\t" + completion_time[i]); } avg_time /= n; System.out.println("Average waiting time: " + avg_time); } } ``` 这段代码实现了简单的作业优先算法,它对输入的进程的到达时间、执行时间进行了排序,并统计并输出每个进程的等待时间、周转时间、完成时间和平均等待时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值