Page 103 - DCAP103_Principle of operating system
P. 103
Principles of Operating Systems
Notes
3.7 An Overview of Thread Scheduling
We will start by looking at the basic principles of how threads are scheduled. Any particular
virtual machine (and underlying operating system) may not follow these principles exactly, but
the principles form the basis for our understanding of thread scheduling. Let us start by looking
at an example with some CPU-intensive threads. In this and subsequent units, we will consume
CPU resources with a recursive Fibonacci number generator, which has the advantage (for our
purposes) of being an elegant and very slow program:
import java.util.*;
import java.text.*;
public class Task implements Runnable {
long n;
String id;
private long fib(long n) {
if (n == 0)
return 0L;
if (n == 1)
return 1L;
return fib(n - 1) + fib(n - 2);
}
public Task(long n, String id) {
this.n = n;
this.id = id;
}
public void run( ) {
Date d = new Date( );
DateFormat df = new SimpleDateFormat(“HH:mm:ss:SSS”);
long startTime = System.currentTimeMillis( );
d.setTime(startTime);
System.out.println(“Starting task” + id + “ at ” + df.format(d));
fib(n);
96 LOVELY PROFESSIONAL UNIVERSITY