Using the getThreadGroup() Method of Thread

Chapter 11 - Self-Running Objects

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Using an Inner Class to Hide run()
In the previous example, steps had to be taken to protect the class from erroneous invocations of run() . Although the class is adequately protected from external intrusion, a user of the class can still fall prey to the lure of calling run() . Or perhaps a user might notice that the class implements Runnable and be tempted to create a Thread and start it. In both cases, a quite unexpected RuntimeException will be thrown.
A better approach available to JDK 1.1 and later developers is to use an inner class to hide the implementation of run() from external code. The class InnerSelfRun , shown in Listing 11.3, demonstrates how this can be done.
Listing 11.3  InnerSelfRun.javaHiding the run() Method in an Inner Class
1: public class InnerSelfRun extends Object {
2:     private Thread internalThread;
3:     private volatile boolean noStopRequested;
4:
5:     public InnerSelfRun() {
6:         // other constructor stuff should appear here first ...
7:         System.out.println(in constructor - initializing...);
8:
9:         // before returning, the thread should be started.
10:         noStopRequested = true;
11:
12:         Runnable r = new Runnable() {
13:                 public void run() {
14:                     try {
15:                         runWork();
16:                     } catch (Exception x) {
17:                         // in case ANY exception slips through
18:                         x.printStackTrace();
19:                     }
20:                 }
21:             };
22:
23:         internalThread = new Thread(r);
24:         internalThread.start();
25:     }
26:
27:     private void runWork() {
28:         while (noStopRequested) {
29:             System.out.println(in runWork() - still going...);
30:
31:             try {
32:                 Thread.sleep(700);
33:             } catch (InterruptedException x) {
34:                 // Any caught interrupts should be reasserted
35:                 // for any blocking statements which follow.
36:                 Thread.currentThread().interrupt();
37:             }
38:         }
39:     }
40:
41:     public void stopRequest() {
42:         noStopRequested = false;
43:         internalThread.interrupt();
44:     }
45:
46:     public boolean isAlive() {
47:         return internalThread.isAlive();
48:     }
49: }
Most of InnerSelfRun is the same as SelfRun , but there are a few key differences. The biggest change is that InnerSelfRun does not implement Runnable (line 1), but instead uses an anonymous, inner class (lines 1221) to create a Runnable . The Runnable that is created is passed as a parameter to the constructor of Thread (line 23). In the inner class, run() is implemented (lines 1320). The private method runWork() is called (line 15) from within a try / catch block. Any exception that slips all the way up through runWork() is caught (line 16) and has its stack trace printed (line 18). Because run() is implemented in an inner class, it is not accessible from the outside, and there is no danger of its being accidentally called. This inner classs run() method can access the private method runWork() of its enclosing class, but no code outside of InnerSelfRun can accidentally call runWork() .
There is no longer any need to check which thread is calling runWork() because it cannot be called from outside this class (no checking between lines 27 and 28). The message printed in the while loop is slightly different (line 29) than before. Otherwise, the remainder of the InnerSelfRun class definition is the same as SelfRun .
InnerSelfRunMain (Listing 11.4) simply creates an InnerSelfRun (line 3), lets it run for 3 seconds (line 5), and then requests that it stop soon (line 7). As before, all that I had to do was instantiate an InnerSelfRun . I didnt have, nor did I need to have, any knowledge that it was an active object.
Listing 11.4  InnerSelfRunMain.javaDemonstration Code for InnerSelfRun
1: public class InnerSelfRunMain extends Object {
2:     public static void main(String[] args) {
3:         InnerSelfRun sr = new InnerSelfRun();
4:
5:         try { Thread.sleep(3000); } catch (InterruptedException x) { }
6:
7:         sr.stopRequest();
8:     }
9: }
When InnerSelfRunMain is run, it produces the following output (your output should be the same):
in constructor - initializing...
in runWork() - still going...
in runWork() - still going...
in runWork() - still going...
in runWork() - still going...
in runWork() - 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