Using Thread.currentThread()

Chapter 3 - Creating and Starting a Thread

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Enlivening a Thread: start() and isAlive()
The start() method signals the thread scheduler that this new thread is ready to go and should have its run() method invoked at the schedulers earliest convenience. Because of the nondeterministic nature of multithreaded programming, one cannot be sure just when the thread will begin execution, but only that it should begin soon.
The API for start() is
public native synchronized void start()
            throws IllegalThreadStateException
If the Thread has already been started, an IllegalThreadStateException will be thrown. When the start() method of a Thread is invoked, the new thread is considered to come alive. The thread remains alive until the run() method returns or until the thread is abruptly stopped by the stop() method (which is a deprecated method, as of JDK 1.2!).
The method
public final native boolean isAlive()
can be used on Thread to test whether a thread has been started and is still running. Listing 3.7 shows an example of how isAlive() can be used.
Listing 3.7  TwoThreadAlive.javaUsing isAlive()
1: public class TwoThreadAlive 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:         TwoThreadAlive tt = new TwoThreadAlive();
17:         tt.setName(my worker thread);
18:
19:         System.out.println(before start(), tt.isAlive()= +
                    [ccc]tt.isAlive());
20:         tt.start();
21:         System.out.println(just after start(), tt.isAlive()= +
                    [ccc]tt.isAlive());
22:
23:         for (int i = 0; i < 10; i++) {
24:             tt.printMsg();
25:         }
26:
27:         System.out.println(
28:             at the end of main(), tt.isAlive()= + tt.isAlive());
29:     }
30: }
The code on line 19 checks whether the new Thread object is alive. This will always be false because the thread has not yet been started. Immediately after the thread is started, another check is done (line 20). At this point, the new Thread object will always be alive. At the end of main() , one last check is done (lines 27 and 28). Sometimes, the Thread object will still be alive, and other times it will have already died, depending on the exact thread scheduling that occurs.
Listing 3.8 presents output from a particular run of TwoThreadAlive . Nearly every time this is run, it gives slightly different output. In this case, note that three messages are printed from the worker thread before the main thread has a chance to execute the isAlive() check right after start() . The worker thread keeps its lead and finishes its workand is therefore no longer alivewell before the main is done, so the check at the end shows that the worker thread is no longer alive.
Listing 3.8  Possible Output from TwoThreadAlive
before start(), tt.isAlive()=false
name=my worker thread
name=my worker thread
name=my worker thread
just after start(), tt.isAlive()=true
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
name=main
name=main
name=main
at the end of main(), tt.isAlive()=false

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