Java Reference
In-Depth Information
9 // Create an instance of this object
10 // Create 4 threads, marking number 1 as a very low priority
11 for(int i = 0; I < 4; i++) {
12 //create a runner
13 Runner r = new Runner();
14 r.setPriority(Thread.MAX_PRIORITY);
15
16 //set the first thread to starve
17 if (i == 0) {
18 r.setPriority(Thread.MIN_PRIORITY);
19 r.setName("Starvation Thread");
20 }
21 //start the thread.
22 r.start();
23 }
24
25 // Exit as soon as we possibly can
26 System.exit(0);
27 }
28
29 /**
30 * Create a thread, then cycle through its command ten times.
31 */
32 static class Runner extends Thread {
33 public void run() {
34 for (int count = 10; count > 0; count--) {
35 System.out.println(getName() + ": is working " + count);
36 }
37 }
38 }
39 }
The starving thread never ran, as you can see in Figure 4-9.
Search WWH ::




Custom Search