KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > thread > v1 > TimedProcess


1 /*
2  * @(#)TimedProcess.java
3  *
4  * Copyright (C) 2001,2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.thread.v1;
28
29
30 /**
31  * A Utility to run a task in a thread, and to kill it if the thread runs
32  * too long. This will first attempt an interrupt() on the thread. If that
33  * doesn't work, then a stop() will be executed. This is a well-documented
34  * bad practice, so you have been warned!
35  *
36  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
37  * @version $Date: 2003/02/10 22:52:49 $
38  * @since 15-Mar-2002
39  */

40 public class TimedProcess
41 {
42     protected static TimedProcess s_instance = new TimedProcess();
43     
44     public static final int REASONABLE_TIME = 500;
45     
46     /**
47      * Implement this interface to have your own method for killing the
48      * runner and thread. The caller will perform the Thread.join() manually.
49      * If the thread is still running after every possible attempt to stop
50      * the thread has occured (run this interface, and join), then a stop()
51      * will be called on the thread.
52      */

53     public static interface RunnableKiller
54     {
55         public void killRunnable( Runnable JavaDoc r, Thread JavaDoc t );
56     }
57     
58     
59     private static class InterruptRunnableKiller implements RunnableKiller
60     {
61         public void killRunnable( Runnable JavaDoc r, Thread JavaDoc t )
62         {
63             t.interrupt();
64         }
65     }
66     
67     private RunnableKiller interruptor = new InterruptRunnableKiller();
68     
69     
70     
71     protected TimedProcess()
72     {
73         // do nothing
74
}
75     
76     
77     public static TimedProcess getInstance()
78     {
79         return s_instance;
80     }
81     
82     
83     /**
84      * Runs the given process in a Thread. If it does not stop within the
85      * specified time, then attempts will be made to kill the thread
86      * (see earlier text). If the process did not complete in the specified
87      * time, an <tt>InterruptedException</tt> will be thrown.
88      *
89      * @exception InterruptedException if the process did not complete within
90      * the given time frame.
91      */

92     public void runTimed( Runnable JavaDoc process, long waitTimeMillis )
93             throws InterruptedException JavaDoc
94     {
95         runTimed( process, waitTimeMillis, this.interruptor );
96     }
97     
98     
99     /**
100      * Runs the given process in a Thread. If it does not stop within the
101      * specified time, then attempts will be made to kill the thread
102      * (see earlier text). If the process did not complete in the specified
103      * time, an <tt>InterruptedException</tt> will be thrown.
104      *
105      * @exception InterruptedException if the process did not complete within
106      * the given time frame.
107      */

108     public void runTimed( Runnable JavaDoc process, long waitTimeMillis,
109             RunnableKiller processEnder )
110             throws InterruptedException JavaDoc
111     {
112         if (processEnder == null || process == null ||
113             waitTimeMillis <= 0L)
114         {
115             throw new IllegalArgumentException JavaDoc("no null args");
116         }
117         
118         Thread JavaDoc t = new Thread JavaDoc( process, "TimedProcess" );
119         setupThread( t );
120         t.start();
121         joinThread( t, waitTimeMillis );
122         if (t.isAlive())
123         {
124             stopThread( t, process, processEnder );
125             
126             throw new InterruptedException JavaDoc( "Process did not end within "+
127                 waitTimeMillis+" milliseconds." );
128         }
129     }
130     
131     
132     
133     /**
134      *
135      */

136     protected void setupThread( Thread JavaDoc t )
137     {
138         // do nothing
139
}
140     
141     
142     /**
143      *
144      */

145     protected void stopThread( Thread JavaDoc t, Runnable JavaDoc process,
146             RunnableKiller processEnder )
147     {
148         processEnder.killRunnable( process, t );
149         joinThread( t, REASONABLE_TIME );
150         if (t.isAlive())
151         {
152             // We'll give it one more shot before we really kill it.
153
t.interrupt();
154             joinThread( t, REASONABLE_TIME );
155             if (t.isAlive())
156             {
157                 // ok, that's enough.
158
// Let's do this the hard way.
159
t.stop(); // we know this is bad, but do it anyway
160
if (t.isAlive())
161                 {
162                     throw new IllegalStateException JavaDoc(
163                         "After stopping the thread, the thread still lives. "+
164                         "The thread cannot be killed." );
165                 }
166             }
167         }
168     }
169     
170     
171     protected void joinThread( Thread JavaDoc t, long joinTime )
172     {
173         try
174         {
175             t.join( joinTime );
176         }
177         catch (InterruptedException JavaDoc e)
178         {
179             // ignore
180
}
181     }
182 }
183
184
Popular Tags