title: ExecutorService that interrupts tasks after a timeout
body: <p>I'm looking for an <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html" rel="noreferrer">ExecutorService</a> implementation that can be provided with a timeout. Tasks that are submitted to the ExecutorService are interrupted if they take longer than the timeout to run. Implementing such a beast isn't such a difficult task, but I'm wondering if anybody knows of an existing implementation.</p>
  
  <p>Here's what I came up with based on some of the discussion below. Any comments?</p>
  
  <pre><code>import java.util.List;
  import java.util.concurrent.*;
  
  public class TimeoutThreadPoolExecutor extends ThreadPoolExecutor {
      private final long timeout;
      private final TimeUnit timeoutUnit;
  
      private final ScheduledExecutorService timeoutExecutor = Executors.newSingleThreadScheduledExecutor();
      private final ConcurrentMap&lt;Runnable, ScheduledFuture&gt; runningTasks = new ConcurrentHashMap&lt;Runnable, ScheduledFuture&gt;();
  
      public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue, long timeout, TimeUnit timeoutUnit) {
          super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
          this.timeout = timeout;
          this.timeoutUnit = timeoutUnit;
      }
  
      public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue, ThreadFactory threadFactory, long timeout, TimeUnit timeoutUnit) {
          super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
          this.timeout = timeout;
          this.timeoutUnit = timeoutUnit;
      }
  
      public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue, RejectedExecutionHandler handler, long timeout, TimeUnit timeoutUnit) {
          super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
          this.timeout = timeout;
          this.timeoutUnit = timeoutUnit;
      }
  
      public TimeoutThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, long timeout, TimeUnit timeoutUnit) {
          super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
          this.timeout = timeout;
          this.timeoutUnit = timeoutUnit;
      }
  
      @Override
      public void shutdown() {
          timeoutExecutor.shutdown();
          super.shutdown();
      }
  
      @Override
      public List&lt;Runnable&gt; shutdownNow() {
          timeoutExecutor.shutdownNow();
          return super.shutdownNow();
      }
  
      @Override
      protected void beforeExecute(Thread t, Runnable r) {
          if(timeout &gt; 0) {
              final ScheduledFuture&lt;?&gt; scheduled = timeoutExecutor.schedule(new TimeoutTask(t), timeout, timeoutUnit);
              runningTasks.put(r, scheduled);
          }
      }
  
      @Override
      protected void afterExecute(Runnable r, Throwable t) {
          ScheduledFuture timeoutTask = runningTasks.remove(r);
          if(timeoutTask != null) {
              timeoutTask.cancel(false);
          }
      }
  
      class TimeoutTask implements Runnable {
          private final Thread thread;
  
          public TimeoutTask(Thread thread) {
              this.thread = thread;
          }
  
          @Override
          public void run() {
              thread.interrupt();
          }
      }
  }
  </code></pre>