KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > util > TimeoutController


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/TimeoutController.java,v 1.4.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.4.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.util;
33
34 /**
35  * <p>
36  * Executes a task with a specified timeout.
37  * </p>
38  * @author Ortwin Glück
39  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
40  * @version $Revision: 1.4.2.1 $
41  * @since 2.0
42  */

43
44 public final class TimeoutController {
45
46     /**
47      * Do not instantiate objects of this class. Methods are static.
48      */

49     private TimeoutController() {
50     }
51
52     /**
53      * Executes <code>task</code>. Waits for <code>timeout</code>
54      * milliseconds for the task to end and returns. If the task does not return
55      * in time, the thread is interrupted and an Exception is thrown.
56      * The caller should override the Thread.interrupt() method to something that
57      * quickly makes the thread die or use Thread.isInterrupted().
58      * @param task The thread to execute
59      * @param timeout The timeout in milliseconds. 0 means to wait forever.
60      * @throws TimeoutException if the timeout passes and the thread does not return.
61      */

62     public static void execute(Thread JavaDoc task, long timeout) throws TimeoutException {
63         task.start();
64         try {
65             task.join(timeout);
66         } catch (InterruptedException JavaDoc e) {
67             /* if somebody interrupts us he knows what he is doing */
68         }
69         if (task.isAlive()) {
70             task.interrupt();
71             throw new TimeoutException();
72         }
73     }
74
75     /**
76      * Executes <code>task</code> in a new deamon Thread and waits for the timeout.
77      * @param task The task to execute
78      * @param timeout The timeout in milliseconds. 0 means to wait forever.
79      * @throws TimeoutException if the timeout passes and the thread does not return.
80      */

81     public static void execute(Runnable JavaDoc task, long timeout) throws TimeoutException {
82         Thread JavaDoc t = new Thread JavaDoc(task, "Timeout guard");
83         t.setDaemon(true);
84         execute(t, timeout);
85     }
86
87     /**
88      * Signals that the task timed out.
89      */

90     public static class TimeoutException extends Exception JavaDoc {
91         /** Create an instance */
92         public TimeoutException() {
93         }
94     }
95 }
96
Popular Tags