Overriding the run() Method

Chapter 2 - A Simple Two-Thread Example

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Putting It All Together
Combining the preceding code and adding a second 10-iteration loop for the main thread to run produces the complete code for TwoThread.java , shown in Listing 2.1.
Listing 2.1  TwoThread.javaThe Complete Code for the TwoThread Example
1: public class TwoThread extends Thread {
2:     public void run() {
3:         for (int i = 0; i < 10; i++) {
4:             System.out.println(New thread);
5:         }
6:     }
7:
8:     public static void main(String[] args) {
9:         TwoThread tt = new TwoThread();
10:         tt.start();
11:
12:         for (int i = 0; i < 10; i++) {
13:             System.out.println(Main thread);
14:         }
15:     }
16: }
First, note that the new class TwoThread directly extends Thread , so it IS-A Thread and takes on all the capabilities of its superclass.
Application execution begins with the main thread, which is spawned by the JavaVM for all applications at startup, entering the main() method (line 8). It proceeds to create a new TwoThread instance (line 9). Next, it spawns a new thread of execution by invoking the start() method (line 10). This new thread will begin its execution by invoking the run() method (line 2) of TwoThread .
At this point, two threads are ready to run, and the thread scheduler runs each thread for short periods, alternating between them. If this switching back and forth is fast enough, they appear to be running simultaneously .
After the main thread spawns the new thread, the main thread proceeds into its loop (lines 1214) and prints Main thread to the console 10 times. When it is done with the loop, it falls through and returns from main() . The main thread dies when it returns from main() .
At approximately the same time, the new thread enters the run() method (line 2), proceeds into its loop (lines 35), and prints New thread to the console 10 times. When it is done with the loop, it falls through and returns from run() . The new thread dies when it returns from run() .
When both threads have completed their work and died, the JavaVM shuts down and the application is done.
Listing 2.2 shows possible output from running this application (your output might differ ). During this run of the application, the messages from the two threads happened to be perfectly interleaved, starting with the message from the main thread. There is no guarantee that this output would be the same if the application was run again. Thread scheduling is nondeterministic and depends on many factors, including what else is currently running in the operating system. The thread scheduler makes what seem to be random decisions about how long each thread should be allowed to run between context switches. The only thing that can be counted on from running this application is that each message will print exactly 10 times, regardless of the order of the messages.
Listing 2.2  Possible Output from a Run of TwoThread.java
Main thread
New thread
Main thread
New thread
Main thread
New thread
Main thread
New thread
Main thread
New thread
Main thread
New thread
Main thread
New thread
Main thread
New thread
Main thread
New thread
Main thread
New thread

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net