Java Reference
In-Depth Information
// Pushing Go button leads to the invocation of this button
public void start () {
// Create 3 instances of each of the Thread subclass
IntCounter ic1 = new IntCounter (1, this);
IntCounter ic2 = new IntCounter (2, this);
IntCounter ic3 = new IntCounter (3, this);
// Start the threads
ic1.start ();
ic2.start ();
ic3.start ();
} // start
} // class NonInteractApplet
In the output shown in Figure 8.3 you see that the threads can finish in a different
order for each press of “Go . The order depends on the time allocated to each
thread and on what kind of thread scheduling the JVM uses. You can experiment
with different thread priorities by adding code to set the priorities of the three
thread instances differently. For instance, set ic1 to a high priority and ic3 to a
low priority before starting the threads
ic1.setPriority (Thread.MAX - PRIORITY);
ic2.setPriority (Thread.NORM - PRIORITY);
ic3.setPriority (Thread.MIN - PRIORITY);
Figure 8.3 Display of the
NonInteractApplet
program. The Go button
has been pushed several
times to illustrate the
different order in which
the threads finish.
 
Search WWH ::




Custom Search