Summary

Chapter 3 - Creating and Starting a Thread

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Using Thread.currentThread()
At times, its useful to know which of the threads currently running in the Java Virtual Machine (JavaVM) is executing a segment of code. In multithreaded programming, more than one thread may enter a method and execute the statements within it.
In the TwoThread example from Chapter 2 , two threads execute the code in the println() method of the PrintStream object referred to by the System.out reference:
System.out.println(Main thread);
System.out.println(New thread);
This one PrintStream object is set up by the JavaVM during initialization for use by any class at any time in a Java program through the System.out reference. Any running thread can invoke the println() method. In this case, the println() method does not care which thread invoked it; it simply takes the String passed in and prints it.
In the Thread API, you can use the static method
public static native Thread currentThread()
to determine which thread is executing a segment of code. You can use this
Creating and Starting a Thread
  Note Many of the methods in Thread are listed with some of the following modifiers: native , final , static , and synchronized . As a quick review, native methods are implemented in non-Java code (typically C or C++ in the JDK). Methods declared to be final may not be overridden in a subclass. When a method is static , it does not pertain to a particular instance of the class, but operates at the class level. The f3 synchronized modifier guarantees that no more than one thread is allowed to execute the statements inside a method at one time. Later in this book, the synchronized modifier is explained in detail.
As an example, look at a new version of TwoThread in Listing 3.1. This example is a bit ridiculous for real-world applications, but serves to illustrate a use of Thread.currentThread() .
Listing 3.1  TwoThread.javaA Version of TwoThread That Uses currentThread()
1: public class TwoThread extends Thread {
2:     private Thread creatorThread;
3:
4:     public TwoThread() {
5:         // make a note of the thread that constructed me!
6:         creatorThread = Thread.currentThread();
7:     }
8:
9:     public void run() {
10:         for (int i = 0; i < 10; i++) {
11:             printMsg();
12:         }
13:     }
14:
15:     public void printMsg() {
16:         // get a reference to the thread running this
17:         Thread t = Thread.currentThread();
18:
19:         if (t == creatorThread) {
20:             System.out.println(Creator thread);
21:         } else if (t == this) {
22:             System.out.println(New thread);
23:         } else {
24:             System.out.println(Mystery thread unexpected!);
25:         }
26:     }
27:
28:     public static void main(String[] args) {
29:         TwoThread tt = new TwoThread();
30:         tt.start();
31:
32:         for (int i = 0; i < 10; i++) {
33:             tt.printMsg();
34:         }
35:     }
36: }
In this version, the System.out.println() statements have been removed from the loops and replaced by a call to the new printMsg() method (lines 11 and 33). This method does not take a String as a parameter, but instead determines which message to print, based on the thread that invokes it.
In the constructor (line 6), Thread.currentThread() is used to gain a reference to the Thread object for the thread that executed the new TwoThread(); statement.
This Thread reference is stored in the member variable creatorThread for later use.
To determine which message to print, printMsg() first gets a reference to the Thread that invoked it, by using the static method Thread.currentThread() (line 17). Next, it tests whether this reference t matches the creatorThread reference stored by the constructor. If so, it prints Creator thread (lines 19 and 20). If the reference doesnt match, printMsg() then checks whether t matches this . TwoThread IS-A Thread because it directly subclasses Thread . The this reference refers to the Thread object constructed on line 29 by the main thread. If the current thread equals this , New thread is printed (lines 21 and 22). Otherwise, another thread that was not accounted for invoked this method, and the message Mystery thread --unexpected! is printed (lines 23 and 24). In this example, the Mystery thread --unexpected! message will never be printed because only two threads will run this code and they have both been accounted for.
Listing 3.2 presents possible output from running TwoThread . Remember that the exact order of the messages printed, as well as how long each thread will run between context switches, depends on the thread scheduler. Therefore, your output might differ somewhat.
Listing 3.2  Possible Output from TwoThread Using currentThread()
Creator thread
New thread
Creator thread
New thread
Creator thread
New thread
Creator thread
New thread
Creator thread
New thread
Creator thread
New thread
Creator thread
New thread
Creator thread
New thread
Creator thread
New thread
Creator 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