www.pudn.com > Scheduler.rar > Scheduler.java~1~


package Scheduler;
import java.util.*;
/**
* TestScheduler.java
*
* This program demonstrates how the scheduler operates.
* This creates the scheduler and then the three example threads.
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/

public class TestScheduler
{
public static void main(String args[]) {
/**
* This must run at the highest priority to ensure that
* it can create the scheduler and the example threads.
* If it did not run at the highest priority, it is possible
* that the scheduler could preempt this and not allow it to
* create the example threads.
*/
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

Scheduler CPUScheduler = new Scheduler();

CPUScheduler.start();

TestThread t1 = new TestThread("Thread 1");
t1.start();
CPUScheduler.addThread(t1);

TestThread t2 = new TestThread("Thread 2");
t2.start();
CPUScheduler.addThread(t2);

TestThread t3 = new TestThread("Thread 3");
t3.start();
CPUScheduler.addThread(t3);
}
}




/**
* CircularList.java
*
* This class implements a circular list using the Vector class
* note that elements in a vector with n elements are numbered 0 .. (n-1)
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/



public class CircularList
{
private Vector List;
private int index;

public CircularList() {
List = new Vector(10);
index = 0;
}

/**
* this method returns the next element in the list.
* @return Object
*/
public Object getNext() {
Object nextElement = null;
int lastElement;

if (!List.isEmpty() ) {
if (index == List.size() )
index = 0;

nextElement = List.elementAt(index);

++index;
}

return nextElement;
}

/**
* this method adds an item to the list
* @return void
*/
public void addItem(Object t) {
List.addElement(t);
}

}



/**
* TestThread.java
*
* This thread is used to demonstrate how the scheduler operates.
* This thread runs forever, periodically displaying its name.
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/

class TestThread extends Thread
{
private String name;

public TestThread(String id) {
name = id;
}

public void run() {
/*
* The thread does something
**/
//while(true){
for(int count=0;count<5;count++){
for (int i = 0; i < 50000; i++)
;
System.out.println("I am thread " + name);
}
}
}
/**
* Scheduler.java
*
* This class is a simple round-robin scheduler.
* The idea for this scheduler came from "Java Threads"
* by Oaks and Wong (Oreilly, 1999).
*
* @author Greg Gagne, Peter Galvin, Avi Silberschatz
* @version 1.0 - July 15, 1999.
* Copyright 2000 by Greg Gagne, Peter Galvin, Avi Silberschatz
* Applied Operating Systems Concepts - John Wiley and Sons, Inc.
*/


public class Scheduler extends Thread
{
private CircularList queue;
private int timeSlice;
private static final int DEFAULT_TIME_SLICE = 1000; // 1 second

public Scheduler() {
timeSlice = DEFAULT_TIME_SLICE;
queue = new CircularList();
}

public Scheduler(int quantum) {
timeSlice = quantum;
queue = new CircularList();
}

/**
* adds a thread to the queue
* @return void
*/
public void addThread(Thread t) {
t.setPriority(2);

queue.addItem(t);
}

/**
* this method puts the scheduler to sleep for a time quantum
* @return void
*/
private void schedulerSleep() {
try {
Thread.sleep(timeSlice);
} catch (InterruptedException e) { };
}


public void run() {
Thread current;

// set the priority of the scheduler to the highest priority
this.setPriority(6);
for(int a=0;a<5;a++){
//while(true) {
try {
current = (Thread)queue.getNext();

if ( (current != null) &amt;&amt; (current.isAlive()) ) {

current.setPriority(4);

schedulerSleep();

System.out.println("* * * Context Switch * * * ");

current.setPriority(2);
}

} catch (NullPointerException e3) { } ;
}
}
}