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

📄 simsystem.java

📁 对于CPU线程调度的模拟
💻 JAVA
字号:
package simcpu;

import java.util.LinkedList;
import java.util.Queue;

public class SimSystem {

	public static String policy;

	SimThread runningThread;

	boolean cpuBusy;

	EventDriver driver;

	public Timer timer = new Timer();

	public Queue<SimThread> readyQueue;

	Scheduler scheduler;

	public SimSystem() {
		readyQueue = readyQueueInstace;
		scheduler = schdulerIntance;
	}

	public SimThread schedul() {
		return scheduler.doSchdul(runningThread, readyQueue);
	}

	public SimThread getRunningThread() {
		return runningThread;
	}

	public void setRunningThread(SimThread runningThread) {
		this.runningThread = runningThread;
	}

	public void run(int episode) {
		if (runningThread != null)
			runningThread.run(episode);
	}

	public boolean isCpuBusy() {
		return cpuBusy;
	}

	public void setCpuBusy(boolean cpuBusy) {
		if (!this.cpuBusy && cpuBusy)
			timer.start(driver.getNow());
		if (this.cpuBusy && !cpuBusy)
			timer.stop(driver.getNow());
		this.cpuBusy = cpuBusy;
	}

	public void setDriver(EventDriver driver) {
		this.driver = driver;
	}

	public static void setPolicy(String string) {
		policy = string;
		if ("FCFS".equalsIgnoreCase(policy)) {
			readyQueueInstace = new LinkedList<SimThread>();
			schdulerIntance = new Scheduler() {
				public SimThread doSchdul(SimThread cur, Queue<SimThread> queue) {
					if(cur==null || cur.getCurTask()==null ||cur.getCurTask().remainingTime<=0) return queue.poll();
					else return cur;
				}
			};
		}
		if ("Round Robin".equalsIgnoreCase(policy)) {
			readyQueueInstace = new LinkedList<SimThread>();
			schdulerIntance = new Scheduler() {
				public SimThread doSchdul(SimThread cur, Queue<SimThread> queue) {
					SimThread next = queue.peek();
					return (next != null) ? queue.poll() : cur;
				}
			};
		}
	}

	static Queue<SimThread> readyQueueInstace;

	static interface Scheduler {
		public SimThread doSchdul(SimThread cur, Queue<SimThread> queue);
	}

	static Scheduler schdulerIntance;

	public void load(SimThread thread) {
		int overhead = (runningThread == null || runningThread.pid != thread.pid) ? DataSource.processSwitch
				: DataSource.threadSwitch;
		driver.predict(new OverheadEvent(driver.now+overhead,driver,this,thread));
		this.setCpuBusy(true);
	}
}

⌨️ 快捷键说明

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