title: Executor Service - timeout of thread
body: <p>While I was exploring <code>ExecutorService</code>, I encountered a method <code>Future.get()</code> which accepts the <code>timeout</code>.</p>
  
  <p>The Java doc of this method says</p>
  
  <hr>
  
  <p>Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.</p>
  
  <p>Parameters:</p>
  
  <p><strong>timeout</strong> the maximum time to wait</p>
  
  <p><strong>unit</strong> the time unit of the timeout argument</p>
  
  <hr>
  
  <p>As per my understanding, we are imposing a timeout on the <code>callable</code>, we submit to the <code>ExecutorService</code> so that, my <code>callable</code> will <strong>interrupt</strong> after the specified time(timeout) has passed</p>
  
  <p>But as per below code, the <code>longMethod()</code> seems to be running beyond the timeout(2 seconds), and I am really confused understanding this. Can anyone please point me to the right path?</p>
  
  <pre><code>import java.util.concurrent.Callable;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.TimeUnit;
  
  public class Timeout implements Callable&lt;String&gt; {
  
      public void longMethod() {
          for(int i=0; i&lt; Integer.MAX_VALUE; i++) {
              System.out.println("a");
          }
      }
  
      @Override
      public String call() throws Exception {
          longMethod();
          return "done";
      }
  
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          ExecutorService service = Executors.newSingleThreadExecutor();
  
          try {
              service.submit(new Timeout()).get(2, TimeUnit.SECONDS);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  
  }
  </code></pre>