title: ThreadPoolExecutor timeout configuration
body: <p>I want to setup a timeout for my ThreadPoolExecutor.</p>
  
  <p>I know that i can use </p>
  
  <pre><code>future.get(THREAD_TIMEOUT_MS, TimeUnit.MILLISECONDS);
  </code></pre>
  
  <p>But this code is blocking.</p>
  
  <p>What i want to achieve is that i can create several Runables which are processed with a pool of 4 threads for example. </p>
  
  <p>If the processing of the thread takes more than e.g. 5 seconds i want to throw a timeout exception.</p>
  
  <p>This is my current setup:</p>
  
  <pre><code>public class ServerExecutorService {
  
      public static int QUEUE_KEEP_ALIVE_TIME_SECONDS = 5;
      public static int MAXIMUM_POOL_SIZE = 12;
      public static int CORE_POOL_SIZE = 12;
      public static Logger LOG = LoggerFactory.getLogger(ServerExecutorService .class);
      public static int THREAD_TIMEOUT_MS = 5000;
      private LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(2);
      private RejectedHandler rejectedHandler = new RejectedHandler();
  
  
      private ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, QUEUE_KEEP_ALIVE_TIME_SECONDS, TimeUnit.SECONDS, linkedBlockingQueue);
  
      public ServerExecutorService () {
          executor.setRejectedExecutionHandler(this.rejectedHandler);
      }
  
      public void setRejectedHandler(RejectedHandler rejectedHandler) {
          executor.setRejectedExecutionHandler(rejectedHandler);
      }
  
  
      public void execute(Runnable runnable){
  //        executor.execute(runnable);
  //        Future&lt;?&gt; future = executor.submit(runnable);
          Future&lt;?&gt; future = executor.submit(runnable);
  
          try {
              future.get(THREAD_TIMEOUT_MS, TimeUnit.MILLISECONDS);
          } catch (TimeoutException e) {
              System.out.println("Thread processing timeout.");
              LOG.warn("Thread processing timeout.", e);
          } catch (Exception e) {
              System.out.println("Thread processing error within ServerExecutorService ");
              LOG.error("Thread processing error within ServerExecutorService ", e);
          }
      }
  
  }
  </code></pre>
  
  <p>But like you can see the future.get(THREAD_TIMEOUT_MS, TimeUnit.MILLISECONDS); will wait until the Thread has finished. So the next Threads are not started.</p>
  
  <p>Test:</p>
  
  <pre><code>@Test
      public void testThreadPoolExhausted() {
          serverExecutorService.setRejectedHandler(rejectedHandler);
          for (int i = 0; i &lt; 4; i++) {
              final int finalI = i;
              serverExecutorService.execute(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          System.out.println("do something" + finalI);
                          Thread.sleep(3000);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              });
          }
      }
  </code></pre>
  
  <p>In this test the second thread is started after 3 seconds and not immediatly.</p>