title: ThreadPoolExecutor Vs ExecutorService for service time out use cases
body: <p>I am going to implement Timeout framework between two services. I am looking at <strong>pros &amp; cons</strong> of <code>ThreadPoolExecutor</code> VS  <code>ExecutorService</code> </p>
  
  <p>Code with ExecutorService.</p>
  
  <pre><code>        ExecutorService service = Executors.newFixedThreadPool(10);
          for ( int i=0; i&lt;10; i++){
              MyCallable myCallable = new MyCallable((long)i);
              Future&lt;Long&gt; futureResult = service.submit(myCallable);
              Long result = null;
              try{
                  result = futureResult.get(5000, TimeUnit.MILLISECONDS);
              }catch(TimeoutException e){
                  System.out.println("Time out after 5 seconds");
                  futureResult.cancel(true);
              }catch(InterruptedException ie){
                  System.out.println("Error: Interrupted");
              }catch(ExecutionException ee){
                  System.out.println("Error: Execution interrupted");
              }
              System.out.println("Result:"+result);
          }
          service.shutdown();
  </code></pre>
  
  <p>Code snippet for MyCallable</p>
  
  <pre><code>class MyCallable implements Callable{
      Long id = 0L;
  
      public MyCallable(Long val){
          this.id = val;
      }
  
      public Long call(){
          // **Call a service and get id from the service**
          return id;
      }
  }
  </code></pre>
  
  <p>If I want to implement with <code>ThreadPoolExecutor</code>, I will code in this way</p>
  
  <pre><code>/* Thread pool Executor */
      BlockingQueue queue = new ArrayBlockingQueue(300);
      ThreadPoolExecutor eventsExecutor =
              new ThreadPoolExecutor(1, 10, 60,
                      TimeUnit.SECONDS, queue, new MyRejectionHandler());
  /* I can submit the tasks as for above code example used in future */
  </code></pre>
  
  <p>Now I am looking at pros &amp; cons of using <code>ThreadPoolExecutor</code>  Vs  <code>ExecutorService</code>. Please don't think that this question is duplicate of <a href="https://stackoverflow.com/questions/16161941/exectuorservice-vs-threadpoolexecutor-which-is-using-linkedblockingqueue">ExectuorService vs ThreadPoolExecutor (which is using LinkedBlockingQueue)</a>.</p>
  
  <p><strong>I have some queries after reading above question and hence posted this question.</strong></p>
  
  <ol>
  <li><p>It was recommended to use <code>ExecutorSevice</code> with <code>Executors.XXX methods</code>. If I use <code>Executors.XXX()</code> methods, do I have capabilities to set <code>RejectionHandler</code>, <code>BlockingQueue</code> size etc? If not, Do I have to fall back on <code>ThreadPoolExecutor</code>?</p></li>
  <li><p>Does <code>ThreadPoolExecutor</code> implemented by <code>ExeuctorService</code> offers unbounded queue? I am implementing <code>Timeout</code> framework between two services.</p></li>
  </ol>
  
  <p><strong><em>Which one is best option between these two? Or Do I have other best option ?</em></strong></p>