CS215
Assignment 7, Getting Started: Getting threads to wake up the scheduler
- First, in TestThread, make the burst times "dynamic." That is, as written the burst times are determined in the constructor, when the thread is created, and is then constant for the lifetime of the thread. Instead, put the random number generator in Thread.run(), so that the burst time varies over the lifetime of the thread. Experiment with the parameters of the random number generator until you get a reasonable percentage of threads running to the end of their bursts before a time slice.
Now we want to set up the scheduler and the threads so that if a thread runs to the end of its CPU burst before a time slice is up, it wakes up the scheduler. This part
is the first that requires techniques of mutual exclusion and synchronization.
- Add a boolean instance method sleeping to the Scheduler class. Initialize it to false in the constructor. Include an accessor method that returns the value of sleeping.
- Instead of invoking Thread.sleep(timeSlice), the scheduler should invoke wait(timeSlice). That is, in the method Scheduler.schedulerSleep() replace the call Thread.sleep(timeSlice) with,
synchronized(this)
{
sleeping = true;
wait(timeSlice);
sleeping = false;
}
- In the TestThread.run() method, just after the close of the for-loop, wake up the scheduler if it is asleep (this uses the accessor method you included in step 2). This is accomplished via the following:
synchronized(CPUScheduler)
{
CPUScheduler.notify();
}
Getting all of this to work is a good first step!
Back to CS215 Home Page