Java Reference
In-Depth Information
uler to "do the right thing" even though there is no specifica-
tion of exactly what that is.
The following program illustrates how yield can affect thread scheduling.
The application takes a list of words and creates a thread for each that
is responsible for printing that word. The first parameter to the applic-
ation says whether each thread will yield after each println ; the second
parameter is the number of times each thread should repeat its word.
The remaining parameters are the words to be repeated:
class Babble extends Thread {
static boolean doYield; // yield to other threads?
static int howOften; // how many times to print
private String word; // my word
Babble(String whatToSay) {
word = whatToSay;
}
public void run() {
for (int i = 0; i < howOften; i++) {
System.out.println(word);
if (doYield)
Thread.yield(); // let other threads run
}
}
public static void main(String[] args) {
doYield = new Boolean(args[0]).booleanValue();
howOften = Integer.parseInt(args[1]);
// create a thread for each word
for (int i = 2; i < args.length; i++)
new Babble(args[i]).start();
}
}
 
Search WWH ::




Custom Search