1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/TimeoutController.java,v 1.6 2004/04/18 23:51:38 jsdever Exp $
  3. * $Revision: 1.6 $
  4. * $Date: 2004/04/18 23:51:38 $
  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. */
  29. package org.apache.commons.httpclient.util;
  30. /**
  31. * <p>
  32. * Executes a task with a specified timeout.
  33. * </p>
  34. * @author Ortwin Glück
  35. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  36. * @version $Revision: 1.6 $
  37. * @since 2.0
  38. */
  39. public final class TimeoutController {
  40. /**
  41. * Do not instantiate objects of this class. Methods are static.
  42. */
  43. private TimeoutController() {
  44. }
  45. /**
  46. * Executes <code>task</code>. Waits for <code>timeout</code>
  47. * milliseconds for the task to end and returns. If the task does not return
  48. * in time, the thread is interrupted and an Exception is thrown.
  49. * The caller should override the Thread.interrupt() method to something that
  50. * quickly makes the thread die or use Thread.isInterrupted().
  51. * @param task The thread to execute
  52. * @param timeout The timeout in milliseconds. 0 means to wait forever.
  53. * @throws TimeoutException if the timeout passes and the thread does not return.
  54. */
  55. public static void execute(Thread task, long timeout) throws TimeoutException {
  56. task.start();
  57. try {
  58. task.join(timeout);
  59. } catch (InterruptedException e) {
  60. /* if somebody interrupts us he knows what he is doing */
  61. }
  62. if (task.isAlive()) {
  63. task.interrupt();
  64. throw new TimeoutException();
  65. }
  66. }
  67. /**
  68. * Executes <code>task</code> in a new deamon Thread and waits for the timeout.
  69. * @param task The task to execute
  70. * @param timeout The timeout in milliseconds. 0 means to wait forever.
  71. * @throws TimeoutException if the timeout passes and the thread does not return.
  72. */
  73. public static void execute(Runnable task, long timeout) throws TimeoutException {
  74. Thread t = new Thread(task, "Timeout guard");
  75. t.setDaemon(true);
  76. execute(t, timeout);
  77. }
  78. /**
  79. * Signals that the task timed out.
  80. */
  81. public static class TimeoutException extends Exception {
  82. /** Create an instance */
  83. public TimeoutException() {
  84. }
  85. }
  86. }