Chapter 3: Creating and Starting a Thread

Chapter 3 - Creating and Starting a Thread

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Naming a Thread: getName() and setName()
Every Thread has a name associated with it. If a name is not explicitly supplied, a default one is generated during construction. By name , you can differentiate between the various threads running in the JavaVM.
Using getName()
In the Thread API, the method
public final String getName()
is used to retrieve the current name. Listing 3.3 shows a new class, TwoThreadGetName , that uses the getName() method to differentiate between two running threads.
Listing 3.3  TwoThreadGetName.javaUsing getName()
1: public class TwoThreadGetName extends Thread {
2:     public void run() {
3:         for (int i = 0; i < 10; i++) {
4:             printMsg();
5:         }
6:     }
7:
8:     public void printMsg() {
9:         // get a reference to the thread running this
10:         Thread t = Thread.currentThread();
11:         String name = t.getName();
12:         System.out.println(name= + name);
13:     }
14:
15:     public static void main(String[] args) {
16:         TwoThreadGetName tt = new TwoThreadGetName();
17:         tt.start();
18:
19:         for (int i = 0; i < 10; i++) {
20:             tt.printMsg();
21:         }
22:     }
23: }
All printing occurs in printMsg() when it is invoked from the loop in run() (lines 35) and from the loop in main() (lines 1921). First, in printMsg() , a reference to the currently executing Thread is obtained using Thread.currentThread() (line 10). Next, the name of this particular Thread is retrieved through getName() (line 11). Finally, the name is printed (line 12).
Listing 3.4 shows possible output from running TwoThreadGetName . Different output can (and usually does) occur each time this is run. For this particular run, note that the mes-ages alternate between threads at first. However, about midway, three Thread-0 messages are printed consecutively without any messages from the other thread. At the end, the Thread-0 thread has died, and the main thread is able to catch up and print the backlogged messages.
This is a perfect example of the nondeterministic behavior of multithreaded programsa critical issue for skilled Java developers to be aware of. In later chapters, you will learn techniques for ensuring the correctness of multithreaded programsregardless of the order in which the threads happen to be scheduled to run.
Listing 3.4  Possible Output from TwoThreadGetName
name=main
name=Thread-0
name=main
name=Thread-0
name=main
name=Thread-0
name=main
name=Thread-0
name=main
name=Thread-0
name=Thread-0
name=Thread-0
name=main
name=Thread-0
name=Thread-0
name=main
name=Thread-0
name=main
name=main
name=main
In addition to a thread named main , the JavaVM starts up other threads automatically. Table 3.1 lists the names of these threads on each of the three released Java platforms. Each row presents the various names of the same thread.
Table 3.1  Threads Started by the JavaVM
JDK 1.2
JDK 1.1
JDK 1.0
main
main
main
Finalizer
Finalizer thread
Finalizer thread
Reference Handler
(none)
(none)
Signal dispatcher
(none)
(none)
AWT-Windows
AWT-Windows
AWT-Win32
AWT-EventQueue-0
AWT-EventQueue-0
AWT-Callback-Win32
SunToolkit.PostEventQueue-0
(none)
(none)
Screen Updater
Screen Updater
Screen Updater
Note that the Reference Handler , Signal dispatcher , and SunToolkit.PostEventQueue-0 threads are new to JDK 1.2. The threads named main and Finalizer ( Reference Handler and Signal dispatcher for JDK 1.2) are started automatically for every application. The remaining threads are also started by the JavaVM when the application contains any graphical components from AWT or Swing. Therefore, in a JDK 1.2 application with a graphical user interface (GUI), eight threads are automatically started by the JavaVM.
As mentioned previously, the main thread is responsible for starting application execution by invoking the main() method. This is the thread from which most other developer-defined threads are spawned by application code. The Finalizer thread is used by the JavaVM to execute the finalize() method of objects just before they are garbage collected. The AWT-EventQueue-0 thread is more commonly known as the event thread and invokes event-handling methods such as actionPerformed() , keyPressed() , mouseClicked() , and windowClosing() .
Using setName()
In the preceding example, the names associated with threads are their default names. You can explicitly specify the name of a Thread object by using the setName() method:
public final void setName(String newName)
The name of a thread is typically set before the thread is started, but setting the name of a thread already running is also permitted. Two Thread objects are permitted to have the same name, but you should avoid this for clarity. The main thread started by the JavaVM can also have its name changed, but this is also discouraged.
  Tip Although Java requires none of the following, its good practice to follow these conventions when naming threads:
  Invoke setName() on the Thread before start() , and do not rename the Thread after it is started.
  Give each thread a brief, meaningful name when possible.
  Give each thread a unique name.
  Do not change the names of JavaVM threads, such as main .
Listing 3.5 shows a new class, TwoThreadSetName , that uses the setName() method to override the default thread name.
Listing 3.5  TwoThreadSetName.javaUsing setName()
1: public class TwoThreadSetName extends Thread {
2:     public void run() {
3:         for (int i = 0; i < 10; i++) {
4:             printMsg();
5:         }
6:     }
7:
8:     public void printMsg() {
9:         // get a reference to the thread running this
10:         Thread t = Thread.currentThread();
11:         String name = t.getName();
12:         System.out.println(name= + name);
13:     }
14:
15:     public static void main(String[] args) {
16:         TwoThreadSetName tt = new TwoThreadSetName();
17:         tt.setName(my worker thread);
18:         tt.start();
19:
20:         for (int i = 0; i < 10; i++) {
21:             tt.printMsg();
22:         }
23:     }
24: }
The only difference between TwoThreadSetName and TwoThreadGetName is the addition of the statement to set the name of the new thread to my worker thread (line 17) before the thread is started. The name of the main thread remains untouched. Listing 3.6 shows possible output; your output might differ because of nondeterministic thread scheduling.
Listing 3.6  Possible Output from TwoThreadSetName
name=main
name=my worker thread
name=main
name=my worker thread
name=my worker thread
name=main
name=my worker thread
name=main
name=my worker thread
name=main
name=my worker thread
name=main
name=my worker thread
name=main
name=my worker thread
name=my worker thread
name=main
name=my worker thread
name=main
name=main

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