FileDocCategorySizeDatePackage
CPUScheduler.javaAPI DocExample2725Thu Feb 04 16:10:38 GMT 1999None

CPUScheduler

public class CPUScheduler extends Thread

Fields Summary
private CircularList
threads
private Thread
current
private int
timeslice
private static boolean
initialized
private boolean
needThreads
Constructors Summary
public CPUScheduler(int t)

		if (isInitialized())
			throw new SecurityException("Already initialized");
		threads = new CircularList();
		timeslice = t;
		setDaemon(true);
	
Methods Summary
public synchronized voidaddThread(java.lang.Thread t)

		t.setPriority(2);
		threads.insert(t);
		if (needThreads) {
			needThreads = false;
			notify();
		}
	
private static synchronized booleanisInitialized()


	     
		if (initialized)
			return true;
		initialized = true;
		return false;
	
public voidremoveThread(java.lang.Thread t)

		threads.delete(t);
		synchronized(this) {
			if (t == current)
				current = null;
		}
	
public synchronized voidrun()

		setPriority(6);
		while (true) {
			current = (Thread) threads.getNext();
			while (current == null) {
				needThreads = true;
				try {
					wait();
				} catch (Exception e) {}
				current = (Thread) threads.getNext();
			}
			try {
				current.setPriority(4);
			} catch (Exception e) {
				removeThread(current);
				continue;
			}
			try {
				wait(timeslice);
			} catch (InterruptedException ie) {};
			if (current != null) {
				try {
					current.setPriority(2);
				} catch (Exception e) {
					removeThread(current);
				}
			}
		}