⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpu.java

📁 用JAVA实现的
💻 JAVA
字号:
package cpusimu;
import java.util.*;
public class CPU {
	private int totalProcess;
	private int switchT;
	private int switchP;
	private int currentTime;
	private int workTime;
	LinkedList<Thread> ready;
	LinkedList<Thread> finish;
	public CPU(int initotal,int t,int p)
	{
		totalProcess=initotal;
		switchT=t;
		switchP=p;
		currentTime=0;
		workTime=0;
		ready=new LinkedList<Thread>();
		finish=new LinkedList<Thread>();
	}
	public double utilization()
	{
		return ((double)workTime)/((double)currentTime);
	}
	public double trunroundtime()
	{
		return ((double)currentTime)/((double)totalProcess);
	}
	public int getTotal()
	{
		return currentTime;
	}
	private void switchThread(int i)
	{
		if(i==0)
		{
			currentTime+=switchT;
			workTime+=switchT;
		}
		else
		{
			currentTime+=switchP;
			workTime+=switchP;
		}
	}
	public void insert(Thread t)
	{
		boolean con=true;
		int index=0;
		while(con&&index<ready.size())
		{
			if(t.getArrivaltime()>ready.get(index).getArrivaltime())
				index++;
			else
				con=false;
		}
		ready.add(index,t);
	}
	public void attemper()
	{
		Thread run=ready.remove();
		if(currentTime<run.getArrivaltime()){
			currentTime=run.getArrivaltime()+run.getTotal().getFirst().cpuTime;
			run.setContent("At time "+run.getArrivaltime()+": Thread {"+Integer.toString(run.getTID())+"} of Process {"+Integer.toString(run.getPID())+"} moves from {blocked} to {running}");
			}
		else{
			if(currentTime<run.getArrivaltime())
			run.setContent("At time "+run.getArrivaltime()+": Thread {"+Integer.toString(run.getTID())+"} of Process {"+Integer.toString(run.getPID())+"} moves from {blocked} to {ready}");
			run.setContent("At time "+currentTime+": Thread {"+Integer.toString(run.getTID())+"} of Process {"+Integer.toString(run.getPID())+"} moves from {ready} to {running}");
			currentTime+=run.getTotal().getFirst().cpuTime;
			}
		workTime+=run.getTotal().getFirst().cpuTime;
		run.setArrivaltime(currentTime+run.getTotal().getFirst().IOtime);
		run.delete();
		if(run.getTotal().size()==0)
		{
			run.setFinishTime(run.getArrivaltime());
			run.setContent("At time "+run.getFinishtime()+": Thread {"+Integer.toString(run.getTID())+"} of Process {"+Integer.toString(run.getPID())+"} moves from {running} to {terminated}");
			insertFinish(run);
			return;
		}
		run.setContent("At time "+currentTime+": Thread {"+Integer.toString(run.getTID())+"} of Process {"+Integer.toString(run.getPID())+"} moves from {running} to {blocked}");
		if(ready.size()==0)
		{
			insert(run);
			return;
		}
		if(run.getArrivaltime()<ready.getFirst().getArrivaltime())
		{
			insert(run);
			return;
		}
		if(run.getPID()==ready.getFirst().getPID())
			switchThread(0);
		else
			switchThread(1);
		insert(run);
	}
	public void insertFinish(Thread t)
	{
		boolean con=true;
		int index=0;
		while(con&&index<finish.size())
		{
			if(t.getPID()>finish.get(index).getPID())
				index++;
			else
				con=false;
		}
		con=true;
		while(con&&index<finish.size())
		{
			if(t.getTID()>finish.get(index).getTID())
				index++;
			else
				con=false;
		}
		finish.add(index, t);
	}
	public void print(boolean con1,boolean con2)
	{
		System.out.println("Total Time required is "+currentTime+"units.");
		System.out.println("Average turnround time is "+currentTime/totalProcess);
		System.out.println("CPU utilization is "+utilization());
		System.out.println();
		for(int i=0;i<finish.size();i++)
		{
			if(con1){
				System.out.println("Thread "+finish.get(i).getTID()+" of "+"Process "+finish.get(i).getPID());
				System.out.println("arrival time: "+finish.get(i).getFirstarrivetime()+" uints.");
				System.out.println("service time: "+finish.get(i).getServicetime()+" uints.");
				System.out.println("I/O time: "+finish.get(i).getIOtime()+" uints.");
				System.out.println("turnaround time: "+(finish.get(i).getFinishtime()-finish.get(i).getFirstarrivetime())+" uints.");
				System.out.println("finish time "+finish.get(i).getFinishtime()+" uints.");
				System.out.println();
			}
			else
				break;
			if(con1&&con2){
				System.out.println(finish.get(i).getContent());
				System.out.println();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -