Java-Based Round-Robin Scheduler
/*
* Scheduler.java
*/
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
*/
public void addThread(Thread t) {
queue.addItem(t);
}
/*
* this method puts the scheduler to sleep for a time quantum
*/
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);
while (true) {
try {
current = (Thread)queue.getNext();
if ( (current != null) && (current.isAlive()) ) {
current.setPriority(4);
schedulerSleep();
System.out.println("* * * Context Switch * * * ");
current.setPriority(2);
}
} catch (NullPointerException e3) { } ;
}
}
}
/*
* TestScheduler.java
*/
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);
}
}
Java Semaphores
/**
* Semaphore.java
*/
public final class Semaphore
{
public Semaphore() {
value = 0;
}
public Semaphore(int v) {
value = v;
}
public synchronized void P() {
while (value <= 0) {
try {
wait();
}
catch (InterruptedException e) { }
}
value --;
}
public synchronized void V() {
++value;
notify();
}
private int value;
}
Read-Write problem with semaphores
/**
* Database.java
*/
public class Database
{
public Database() {
readerCount = 0;
mutex = new Semaphore(1);
db = new Semaphore(1);
}
// readers and writers will call this to nap
public static void napping() {
int sleepTime = (int) (NAP_TIME * Math.random() );
try { Thread.sleep(sleepTime*1000); }
catch(InterruptedException e) {}
}
public int startRead() {
mutex.P();
++readerCount;
// if I am the first reader tell all others
// that the database is being read
if (readerCount == 1)
db.P();
mutex.V();
return readerCount;
}
public int endRead() {
mutex.P();
--readerCount;
// if I am the last reader tell all others
// that the database is no longer being read
if (readerCount == 0)
db.V();;
mutex.V();
return readerCount;
}
public void startWrite() {
db.P();
}
public void endWrite() {
db.V();
}
// the number of active readers
private int readerCount;
Semaphore mutex; // controls access to readerCount
Semaphore db; // controls access to the database
private static final int NAP_TIME = 15;
}
/**
* Reader.java
*/
public class Reader extends Thread
{
public Reader(int r, Database db)
{
readerNum = r;
server = db;
}
public void run()
{
int c;
while (true)
{
System.out.println("reader " + readerNum + " is sleeping.");
Database.napping();
System.out.println("reader " + readerNum + " wants to read.");
c = server.startRead();
// you have access to read from the database
System.out.println("reader " + readerNum + " is reading. Count = " + c);
Database.napping();
c = server.endRead();
System.out.println("reader " + readerNum + " is done reading. Count = " + c);
}
}
private Database server;
private int readerNum;
}
/**
* Writer.java
*/
public class Writer extends Thread
{
public Writer(int w, Database db) {
writerNum = w;
server = db;
}
public void run() {
while (true)
{
System.out.println("writer " + writerNum + " is sleeping.");
Database.napping();
System.out.println("writer " + writerNum + " wants to write.");
server.startWrite();
// you have access to write to the database
System.out.println("writer " + writerNum + " is writing.");
Database.napping();
server.endWrite();
System.out.println("writer " + writerNum + " is done writing.");
}
}
private Database server;
private int writerNum;
}
/**
* ReaderWriterServer.java
*/
public class ReaderWriterServer
{
public static void main(String args[])
{
Database server = new Database();
Reader[] readerArray = new Reader[NUM_OF_READERS];
Writer[] writerArray = new Writer[NUM_OF_WRITERS];
for (int i = 0; i < NUM_OF_READERS; i++)
{
readerArray[i] = new Reader(i, server);
readerArray[i].start();
}
for (int i = 0; i < NUM_OF_WRITERS; i++)
{
writerArray[i] = new Writer(i, server);
writerArray[i].start();
}
}
private static final int NUM_OF_READERS = 3;
private static final int NUM_OF_WRITERS = 2;
}
Semaphores Class In JDK
There is semaphores class in the JDK on the edlab machines, when you use that semaphore class, you should import corresponding package as the example below, you also can obtain semaphore class from web:
http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
Dinging philosophers problem with semaphores
import EDU.oswego.cs.dl.util.concurrent.*;
/*****************************************************/
/* dining - implements dining philosophers program */
/* using semaphores. 5 philosophers, 5 forks. */
/*****************************************************/
class philosopher extends Thread {
Semaphore fleft, fright;
String name;
philosopher( String who, Semaphore fl, Semaphore fr ) {
fleft = fl;
fright = fr;
name = who;
}
public void run() {
int ThinkTime;
int EatTime;
while (true) {
ThinkTime = (int) (4000*Math.random());
EatTime = (int) (4000*Math.random());
try{
fleft.acquire();
fright.acquire();}catch(InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println(name + " is eating...");
try {
sleep( EatTime );
} catch (InterruptedException e) {};
fright.release();
fleft.release();
try {
sleep( ThinkTime ); // think time
} catch (InterruptedException e) {};
}
}
}import EDU.oswego.cs.dl.util.concurrent.*;
class dining {
public static void main( String[] args ) {
Semaphore f1 = new Semaphore(1);
Semaphore f2 = new Semaphore(1);
Semaphore f3 = new Semaphore(1);
Semaphore f4 = new Semaphore(1);
Semaphore f5 = new Semaphore(1);
philosopher batory = new philosopher( "Batory", f1, f2 );
philosopher lengauer = new philosopher( "Lengauer", f2, f3 );
philosopher porter = new philosopher( "Porter", f3, f4 );
philosopher gouda = new philosopher( "Gouda", f4, f5 );
philosopher kuipers = new philosopher( "Kuipers", f5, f1 );
batory.start();
lengauer.start();
porter.start();
gouda.start();
kuipers.start();
}
}