Finding the Subgroups of a Thread Group

Chapter 11 - Self-Running Objects

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Simple Self-Running Class
The class SelfRun , shown in Listing 11.1, demonstrates a simple example of an active object. During construction, it automatically starts an internal thread running.
Listing 11.1  SelfRun.javaA Simple Self-Running Class
1: public class SelfRun extends Object implements Runnable {
2:     private Thread internalThread;
3:     private volatile boolean noStopRequested;
4:
5:     public SelfRun() {
6:         // other constructor stuff should appear here first ...
7:         System.out.println(in constructor - initializing...);
8:
9:         // Just before returning, the thread should be
10:         // created and started.
11:         noStopRequested = true;
12:         internalThread = new Thread(this);
13:         internalThread.start();
14:     }
15:
16:     public void run() {
17:         // Check that no one has erroneously invoked
18:         // this public method.
19:         if (Thread.currentThread() != internalThread) {
20:             throw new RuntimeException(only the internal +
21:                 thread is allowed to invoke run());
22:         }
23:
24:         while (noStopRequested) {
25:             System.out.println(in run() - still going...);
26:
27:             try {
28:                 Thread.sleep(700);
29:             } catch (InterruptedException x) {
30:                 // Any caught interrupts should be habitually
31:                 // reasserted for any blocking statements
32:                 // which follow.
33:                 Thread.currentThread().interrupt();
34:             }
35:         }
36:     }
37:
38:     public void stopRequest() {
39:         noStopRequested = false;
40:         internalThread.interrupt();
41:     }
42:
43:     public boolean isAlive() {
44:         return internalThread.isAlive();
45:     }
46: }
SelfRun implements Runnable (line 1), as did the earlier examples. The two member variables are used to maintain and control the internal thread. The private variable internalThread (line 2) holds a reference to the thread that is used to run the object. The private variable noStopRequested (line 3) is used as a flag to indicate whether or not the internal thread should continue processing. The internal thread continues to process code as long as noStopRequested is true . It is marked volatile because two different threads access it: the internal thread reads it, and an external thread will change it.
In the constructor (lines 5-14), after the class-specific initialization activities have all been completed (lines 6-7), the noStopRequested flag is initially set to true (line 11). A new Thread object is constructed by passing in this , the reference to the SelfRun object being created. This thread is used as the internal thread for this object and is automatically started (line 13). By the time the constructor returns, an internal thread has been created and has been started. We dont know if run() has been called yet by this internal thread, but the correctness of the design should not depend on its being called before the constructor returns. You should keep in mind that run() might be invoked before the constructor returns, right after it returns, or some time after it returns. The timing is dependent on the whims of the thread scheduler.
In run() (lines 1636), the very first thing done is a check that run() was only invoked by the internal thread (lines 1922). This is necessary to ensure that no one mistakenly invokes run() from outside this class, something that is completely possible because run() is public . If any other thread invokes run() , a RuntimeException is thrown to indicate the error (lines 2021).
The while loop within run() (lines 2435) continues until noStopRequested becomes false . In this example, a simple message is printed each time through just to show the internal activities (line 25). The internal thread then sleeps briefly before looping again (line 28). If this sleep() is interrupted , its InterruptedException is caught right away (line 29). After catching the exception, the internal thread reinterrupts itself (line 33) in case any other interrupt-detecting statements (such as wait() , or another sleep() ) are present before the orderly shutdown can complete. This is a good habit to get into when interrupting a thread is used as a signaling mechanism for stopping.
To request that the internal thread gracefully die as soon as possible, the stopRequest() method is used (lines 3841). When invoked, it sets noStopRequested to false (line 39). stopRequest() then interrupts the internal thread in case it is blocked on an interruptible statement (such as wait() or sleep() ) so that it gets a chance to notice that noStopRequested is now false.
Because a stopRequest() is only a request that the thread die as soon as it has completed any cleanup activities, another method is needed to determine if the thread has died yet. The isAlive() method (lines 4345) is used to proxy the query to the internal thread to determine if it is still alive .
The SelfRunMain class is used to demonstrate the SelfRun class in action. The code for it is in Listing 11.2.
Listing 11.2  SelfRunMain.javaDemonstration Code for SelfRun
1: public class SelfRunMain extends Object {
2:     public static void main(String[] args) {
3:         SelfRun sr = new SelfRun();
4:
5:         try { Thread.sleep(3000); } catch (InterruptedException x) { }
6:
7:         sr.stopRequest();
8:     }
9: }
SelfRunMain simply constructs a SelfRun (line 3), lets it run for 3 seconds (line 5), and then requests that it stop soon (line 7). The main feature to note is that all I had to do was to construct a SelfRun . The hassle of creating a Thread and starting it is gone. In fact, if I dont have a need to ever stop it, I dont even have to be aware that it has a thread running within it!
When SelfRunMain is run, the following output will be produced (your output should match):
in constructor - initializing...
in run() - still going...
in run() - still going...
in run() - still going...
in run() - still going...
in run() - still going...

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