标签:
public class PCB {
//进程的ID
private long id;
//优先级顺序
private int priority;
// 运行的cpu时间
private long cputime;
// 需要运行的时间
private long alltime;
// 即将阻塞的时间
private long startblock;
// 从阻塞中回复的时间
private long blocktime;
// 现在线程的状态
private String state;
public PCB(long id, int priority, long cputime, long alltime,
long startblock, long blocktime, String state) {
super();
this.id = id;
this.priority = priority;
this.cputime = cputime;
this.alltime = alltime;
this.startblock = startblock;
this.blocktime = blocktime;
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public long getBlocktime() {
return blocktime;
}
public void setBlocktime(long blocktime) {
this.blocktime = blocktime;
}
// 运行的三个状态public final static String STATE_READY = "READY";
public final static String STATE_RUN = "RUN";
public final static String STATE_BLOCK = "BLOCK";
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public long getCputime() {
return cputime;
}
public void setCputime(long cputime) {
this.cputime = cputime;
}
public long getAlltime() {
return alltime;
}
public void setAlltime(long alltime) {
this.alltime = alltime;
}
public long getStartblock() {
return startblock;
}
public void setStartblock(long startblock) {
this.startblock = startblock;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
public class OrderImitate {
// 按优先级次序排列的数组
private ArrayList<PCB> priorityArrayList = null;
/**
* 优先级排序
*/
public void sortArray(){
PCB[] pcbs = (PCB[])priorityArrayList.toArray(new PCB[priorityArrayList.size()]);
Arrays.sort(pcbs, new MyComprator());
List<PCB> list = Arrays.asList(pcbs);
this.priorityArrayList = new ArrayList<PCB>(list);
}
/**
* 初始化,按照书中的数据进行初始化操作
*/
private void init(){
priorityArrayList = new ArrayList<PCB>();
PCB zero = new PCB(0, 9, 0, 3, 2, 3, PCB.STATE_READY);
priorityArrayList.add(zero);
PCB one = new PCB(1, 38, 0, 3, -1, 0, PCB.STATE_READY);
priorityArrayList.add(one);
PCB two = new PCB(2, 30, 0, 6, -1, 0, PCB.STATE_READY);
priorityArrayList.add(two);
PCB three = new PCB(3, 29, 0, 3, -1, 0, PCB.STATE_READY);
priorityArrayList.add(three);
PCB four = new PCB(4, 0, 0, 4, -1, 0, PCB.STATE_READY);
priorityArrayList.add(four);
sortArray();
}
/**
* 优先级执行算法
* @return
*/
public boolean hightpPriority(){
// 获得第一个元素
PCB first = priorityArrayList.get(0);
if(!first.getState().equals(PCB.STATE_BLOCK)){
// 运行一次优先级顺序-3
first.setPriority(first.getPriority()-3);
first.setCputime(first.getCputime()+1);
first.setAlltime(first.getAlltime()-1);
// 是否进入阻塞状态
if(first.getStartblock() > 0){
first.setStartblock(first.getStartblock() - 1);
}
// 当是阻塞状态的时候,将状态变为阻塞
if(first.getStartblock() == 0){
first.setState(PCB.STATE_BLOCK);
}
}
// 所需要的时间片完成,结束运行
if(first.getAlltime() == 0){
System.out.println("----------------------------------------------");
System.out.println("ID " + first.getId() + " have done !");
System.out.println("----------------------------------------------");
priorityArrayList.remove(0);
}
// 后面的任务优先级+1
Iterator<PCB> iterator = priorityArrayList.iterator();
int count = 0;
while(iterator.hasNext()){
if(count == 0){
count++;
iterator.next();
continue;
}
PCB pcb = iterator.next();
pcb.setPriority(pcb.getPriority()+1);
// PCB的阻塞状态改变
if(pcb.getBlocktime() != 0 && pcb.getState().equals(PCB.STATE_BLOCK)){
pcb.setBlocktime(pcb.getBlocktime() -1);
if(pcb.getBlocktime() == 0){
pcb.setState(PCB.STATE_READY);
}
}
}
sortArray();
return true;
}
/**
* 执行程序
*/
public void startRuning(){
while(hightpPriority()){
if(this.priorityArrayList.size() == 0){
System.out.println("-----------------> ALL Works have done !");
break;
}
// 输出数据
Iterator<PCB> iterator = priorityArrayList.iterator();
System.out.println("-----------------------------------------------------------------------------------------");
System.out.println("ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE");
while(iterator.hasNext()){
PCB pcb = iterator.next();
System.out
.printf("%d %8d %12d %12d %13d %14d %18s\n", pcb.getId(),
pcb.getPriority(), pcb.getCputime(),
pcb.getAlltime(), pcb.getStartblock(),
pcb.getBlocktime(), pcb.getState());
}
System.out.println("-----------------------------------------------------------------------------------------");
}
}
public static void main(String[] args) {
OrderImitate orderImitate = new OrderImitate();
orderImitate.init();
orderImitate.startRuning();
}
// 优先级次序调整,优先级高且没有阻塞的进程在前面
class MyComprator implements Comparator<PCB> {
public int compare(PCB o1, PCB o2) {
if((o1.getState().equals(PCB.STATE_READY) && o2.getState().equals(PCB.STATE_READY)) || (o1.getState().equals(PCB.STATE_BLOCK) && o2.getState().equals(PCB.STATE_BLOCK))){
if(o1.getPriority() == o2.getPriority() ){
return 0;
}
else if(o1.getPriority() > o2.getPriority()){
return -1;
}else {
return 1;
}
}else if(o1.getState().equals(PCB.STATE_BLOCK) && o2.getState().equals(PCB.STATE_READY)){
return 1;
}else{
return -1;
}
}
}
}
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
1 35 1 2 -1 0 READY
2 31 0 6 -1 0 READY
3 30 0 3 -1 0 READY
0 10 0 3 2 3 READY
4 1 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
1 32 2 1 -1 0 READY
2 32 0 6 -1 0 READY
3 31 0 3 -1 0 READY
0 11 0 3 2 3 READY
4 2 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
----------------------------------------------
ID 1 have done !
----------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
2 32 0 6 -1 0 READY
3 32 0 3 -1 0 READY
0 12 0 3 2 3 READY
4 3 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
3 33 0 3 -1 0 READY
2 29 1 5 -1 0 READY
0 13 0 3 2 3 READY
4 4 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
3 30 1 2 -1 0 READY
2 30 1 5 -1 0 READY
0 14 0 3 2 3 READY
4 5 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
2 31 1 5 -1 0 READY
3 27 2 1 -1 0 READY
0 15 0 3 2 3 READY
4 6 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
2 28 2 4 -1 0 READY
3 28 2 1 -1 0 READY
0 16 0 3 2 3 READY
4 7 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
3 29 2 1 -1 0 READY
2 25 3 3 -1 0 READY
0 17 0 3 2 3 READY
4 8 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
----------------------------------------------
ID 3 have done !
----------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
2 25 3 3 -1 0 READY
0 18 0 3 2 3 READY
4 9 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
2 22 4 2 -1 0 READY
0 19 0 3 2 3 READY
4 10 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
0 20 0 3 2 3 READY
2 19 5 1 -1 0 READY
4 11 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
2 20 5 1 -1 0 READY
0 17 1 2 1 3 READY
4 12 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
----------------------------------------------
ID 2 have done !
----------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
0 17 1 2 1 3 READY
4 13 0 4 -1 0 READY
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
4 14 0 4 -1 0 READY
0 14 2 1 0 3 BLOCK
-----------------------------------------------------------------------------------------
可以看到这里因为线程阻塞产生了调度:
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
4 11 1 3 -1 0 READY
0 15 2 1 0 2 BLOCK
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
4 8 2 2 -1 0 READY
0 16 2 1 0 1 BLOCK
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
0 17 2 1 0 0 READY
4 5 3 1 -1 0 READY
-----------------------------------------------------------------------------------------
----------------------------------------------
ID 0 have done !
----------------------------------------------
-----------------------------------------------------------------------------------------
ID PRIORITY CPUTIME ALLTIME STARTBLOCK BLOCKTIME STATE
4 5 3 1 -1 0 READY
-----------------------------------------------------------------------------------------
----------------------------------------------
ID 4 have done !
----------------------------------------------
-----------------> ALL Works have done !
标签:
原文地址:http://www.cnblogs.com/sober-reflection/p/4192315.html