📄 semscheduler.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -