semscheduler.java

来自「本程序是国外经典的操作系统调度试验」· Java 代码 · 共 54 行

JAVA
54
字号
import java.util.Vector ;public class SemScheduler extends Thread {protected Vector processList ;    public SemScheduler() {	processList = new Vector() ;    }    public void addProcess(SemProc t, boolean starting) {	synchronized(this) { 	    processList.addElement(t) ;	}	if (starting)  // make processes wait to be scheduled	    synchronized(t) {	        try { t.wait() ;} 	        catch (Exception e) { 		    System.out.println("Unexpected wakeup " + e) ;	        }           }    }   public void removeProcess(SemProc t) {	synchronized(t) {	    synchronized(this) {	        processList.remove(t) ;                notify() ;  // it is always the running process which removes itself			// so need to schedule another            }	    try { t.wait() ;} // and send it back to sleep 	    catch (Exception e) { 		System.out.println("Unexpected wakeup " + e) ;	    }	}   }	   public synchronized void run() {	while (true) {	    if (processList.isEmpty()) {		System.out.println("Nothing left to run! Exiting.") ;		System.exit(0) ;	    }	    SemProc nextRunner = (SemProc) processList.elementAt(0) ;	    synchronized(nextRunner) { nextRunner.notify() ; }	    try { wait() ; }  // until that process removes itself	    catch (Exception e) {                 System.out.println("Unexpected interrupt in run " + e) ; 	    }        }    }}

⌨️ 快捷键说明

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