Accidentally Returning Early

Chapter 16 - The SureStop Utility

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Watching It Work with SureStopDemo
SureStopDemo (see Listing 16.3) starts several threads to demonstrate how SureStop works. But instead of SureStop , SureStopVerbose is used so that more detail about what is going on internally is printed. Although the threads running in SureStopDemo could have been stopped in a more orderly fashion by using interrupt() , they are not so that the functionality of SureStop can be fully exercised. SureStopDemo runs for about 25 seconds before automatically exiting the VM.
Listing 16.3  SureStopDemo.javaDemonstration Code for SureStopVerbose
1: public class SureStopDemo extends Object {
2:     private static Thread launch(
3:                 final String name ,
4:                 long lifeTime
5:            ) {
6:
7:         final int loopCount = (int) (lifeTime / 1000);
8:
9:         Runnable r = new Runnable() {
10:                 public void run() {
11:                     try {
12:                         for (int i = 0; i < loopCount; i++) {
13:                             Thread.sleep(1000);
14:                             System.out.println(
15:                                     -> Running - + name);
16:                         }
17:                     } catch (InterruptedException x) {
18:                         // ignore
19:                     }
20:                 }
21:             };
22:        
23:         Thread t = new Thread(r);
24:         t.setName(name);
25:         t.start();
26:
27:         return t;
28:     }
29:
30:     public static void main(String[] args) {
31:         Thread t0 = launch(T0, 1000);
32:         Thread t1 = launch(T1, 5000);
33:         Thread t2 = launch(T2, 15000);
34:
35:         try { Thread.sleep(2000); }
36:         catch (InterruptedException x) { }
37:
38:         SureStopVerbose.ensureStop(t0,  9000);
39:         SureStopVerbose.ensureStop(t1, 10000);
40:         SureStopVerbose.ensureStop(t2, 12000);
41:
42:         try { Thread.sleep(20000); }
43:         catch (InterruptedException x) { }
44:
45:         Thread t3 = launch(T3, 15000);
46:         SureStopVerbose.ensureStop(t3, 5000);
47:
48:         try { Thread.sleep(1000); }
49:         catch (InterruptedException x) { }
50:
51:         Thread t4 = launch(T4, 15000);
52:         SureStopVerbose.ensureStop(t4, 3000);
53:     }
54: }
The launch() method (lines 228) is used to create a new thread and start it running. A name for the thread is passed in along with the amount of time that the thread should keep running before dying on its own. About once per second, the thread prints a message (lines 1216). z reference to the Thread created is returned (line 27).
The main() method launches three of these threads (lines 3133) and then sleeps for 2 seconds (lines 3536). It then specifies that SureStopVerbose should make certain that the threads are stopped (lines 3840). The main() method requests that t0 be stopped in 9 seconds, but t0 should have already died on its own about 1 second ago. It requests that t1 be stopped in 10 seconds, but t1 should stop on its own in about 3 more seconds. It requests that t2 be stopped in 12 seconds, and it will need to be stopped because t2 is scheduled to run for about 13 more seconds. The main thread then sleeps for 20 seconds to allow all this to play out (lines 4243). After this, two more threads are launched and monitored (lines 4552).
Listing 16.4 shows possible output when SureStopDemo is run. Your output should match closely, but some of the timing might be slightly different.
Listing 16.4  Possible Output from SureStopDemo
  1: -> Running - T0
  2: -> Running - T1
  3: -> Running - T2
  4: -> Running - T1
  5: -> Running - T2
  6: main: SureStopVerbose instance created.
  7: SureStopThread: about to sleep for 0.5 seconds
  8: main: entering ensureStop() - name=T0, msGracePeriod=9000
  9: main: already stopped, not added to list - T0
10: main: entering ensureStop() - name=T1, msGracePeriod=10000
11: main: added entry to pendingList, name=T1, stopTime=924307803230, in 9840 ms
12: main: leaving ensureStop() - name=T1
13: main: entering ensureStop() - name=T2, msGracePeriod=12000
14: main: added entry to pendingList, name=T2, stopTime=924307805390, in 12000 ms
15: main: leaving ensureStop() - name=T2
16: SureStopThread: done with sleep for 0.5 seconds
17: SureStopThread: entering checkStopList() - stopList. size ()=0
18: SureStopThread: leaving checkStopList() - stopList.size()=0
19: SureStopThread: back from checkStopList(), sleepTime=9223371112546982037
20: SureStopThread: copying 2 elements from pendingList to stopList
21: SureStopThread: pendingList.size()=0, stopList grew by 2
22: SureStopThread: about to sleep for 0.5 seconds
23: -> Running - T1
24: -> Running - T2
25: SureStopThread: done with sleep for 0.5 seconds
26: SureStopThread: entering checkStopList() - stopList.size()=2
27: SureStopThread: thread is alive - T1
28: SureStopThread: new minTime=924307803230
29: SureStopThread: thread is alive - T2
30: SureStopThread: new minTime=924307803230
31: SureStopThread: leaving checkStopList() - stopList.size()=2
32: SureStopThread: back from checkStopList(), sleepTime=8960
33: SureStopThread: about to wait on pendingList for 8960 ms
34: -> Running - T1
35: -> Running - T2
36: -> Running - T1
37: -> Running - T2
38: -> Running - T2
39: -> Running - T2
40: -> Running - T2
41: -> Running - T2
42: -> Running - T2
43: -> Running - T2
44: -> Running - T2
45: SureStopThread: waited on pendingList for 8960 ms
46: SureStopThread: about to sleep for 0.5 seconds
47: SureStopThread: done with sleep for 0.5 seconds
48: SureStopThread: entering checkStopList() - stopList.size()=2
49: SureStopThread: thread died on its own - T1
50: SureStopThread: thread is alive - T2
51: SureStopThread: new minTime=924307805390
52: SureStopThread: leaving checkStopList() - stopList.size()=1
53: SureStopThread: back from checkStopList(), sleepTime=1620
54: SureStopThread: about to wait on pendingList for 1620 ms
55: -> Running - T2
56: -> Running - T2
57: SureStopThread: waited on pendingList for 1650 ms
58: SureStopThread: about to sleep for 0.5 seconds
59: SureStopThread: done with sleep for 0.5 seconds
60: SureStopThread: entering checkStopList() - stopList.size()=1
61: SureStopThread: thread is alive - T2
62: SureStopThread: timed out, stopping - T2
63: SureStopThread: leaving checkStopList() - stopList.size()=0
64: SureStopThread: back from checkStopList(), sleepTime=9223371112546969897
65: SureStopThread: about to wait on pendingList for 9223371112546969897 ms
66: main: entering ensureStop() - name=T3, msGracePeriod=5000
67: main: added entry to pendingList, name=T3, stopTime=924307818440, in 5000 ms
68: SureStopThread: waited on pendingList for 7530 ms
69: SureStopThread: copying 1 elements from pendingList to stopList
70: SureStopThread: pendingList.size()=0, stopList grew by 1
71: SureStopThread: about to sleep for 0.5 seconds
72: main: leaving ensureStop() - name=T3
73: SureStopThread: done with sleep for 0.5 seconds
74: SureStopThread: entering checkStopList() - stopList.size()=1
75: SureStopThread: thread is alive - T3
76: SureStopThread: new minTime=924307818440
77: SureStopThread: leaving checkStopList() - stopList.size()=1
78: SureStopThread: back from checkStopList(), sleepTime=4510
79: SureStopThread: about to wait on pendingList for 4510 ms
80: main: entering ensureStop() - name=T4, msGracePeriod=3000
81: main: added entry to pendingList, name=T4, stopTime=924307817430, in 3000 ms
82: SureStopThread: waited on pendingList for 500 ms
83: SureStopThread: copying 1 elements from pendingList to stopList
84: SureStopThread: pendingList.size()=0, stopList grew by 1
85: SureStopThread: about to sleep for 0.5 seconds
86: main: leaving ensureStop() - name=T4
87: -> Running - T3
88: SureStopThread: done with sleep for 0.5 seconds
89: SureStopThread: entering checkStopList() - stopList.size()=2
90: SureStopThread: thread is alive - T3
91: SureStopThread: new minTime=924307818440
92: SureStopThread: thread is alive - T4
93: SureStopThread: new minTime=924307817430
94: SureStopThread: leaving checkStopList() - stopList.size()=2
95: SureStopThread: back from checkStopList(), sleepTime=2510
96: SureStopThread: about to wait on pendingList for 2510 ms
97: -> Running - T4
98: -> Running - T3
99: -> Running - T4
100: -> Running - T3
101: -> Running - T4
102: -> Running - T3
103: SureStopThread: waited on pendingList for 2530 ms
104: SureStopThread: about to sleep for 0.5 seconds
105: SureStopThread: done with sleep for 0.5 seconds
106: SureStopThread: entering checkStopList() - stopList.size()=2
107: SureStopThread: thread is alive - T3
108: SureStopThread: new minTime=924307818440
109: SureStopThread: thread is alive - T4
110: SureStopThread: timed out, stopping - T4
111: SureStopThread: leaving checkStopList() - stopList.size()=1
112: SureStopThread: back from checkStopList(), sleepTime=500
113: SureStopThread: about to wait on pendingList for 500 ms
114: -> Running - T3
115: SureStopThread: waited on pendingList for 500 ms
116: SureStopThread: about to sleep for 0.5 seconds
117: SureStopThread: done with sleep for 0.5 seconds
118: SureStopThread: entering checkStopList() - stopList.size()=1
119: SureStopThread: thread is alive - T3
120: SureStopThread: timed out, stopping - T3
121: SureStopThread: leaving checkStopList() - stopList.size()=0
122: SureStopThread: back from checkStopList(), sleepTime=9223371112546956877
123: SureStopThread: about to wait on pendingList for 9223371112546956877 ms
Notice that as predicted , t0 stopped on its own before being monitored by SureStopVerbose (line 9). Monitoring began on t1 (lines 11, 27), but the thread died on its own before timing out (line 49). Similarly, monitoring began on t2 (lines 14, 29, 50, 61), and the thread timed out and was stopped by SureStopVerbose (line 62).

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