title: Stop an infinite loop in an ExecutorService task
body: <pre><code>import java.util.Arrays;
  import java.util.Iterator;
  import java.util.List;
  import java.util.concurrent.Callable;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.Future;
  import java.util.concurrent.TimeUnit;
  
  class Task implements Callable&lt;String&gt; {
      public String call() throws Exception {
          String s = "initial";
          try {
              System.out.println("Started..");
              /*for (int i=0;i&lt;10000;i++) {
                  if (i % 2 == 0) {
                      System.out.println("Even");
                  }
              }*/
              boolean flag = true;
              while(flag) {
  
              }
              System.out.println("Finished!");
              s = "Done";
          }
          catch (RuntimeException e) {
              s = "RuntimeException";
          }
          catch (Exception e) {
              s = "Exception";
          }
          finally {
  
          }
          return s;
      }
  }
  
  public class ExecutorServiceTest {
  
      public static void main(String[] args) throws Exception {
          ExecutorService executor = Executors.newSingleThreadExecutor();
          List&lt;Future&lt;String&gt;&gt; result = executor.invokeAll(Arrays.asList(new Task()), 5, TimeUnit.SECONDS);
          executor.shutdown();
          Iterator&lt;Future&lt;String&gt;&gt; iter = result.iterator();
          while (iter.hasNext()) {
              System.out.println("Came here");
              Future&lt;String&gt; fut = iter.next();
              System.out.println(fut.get());
          }
      }
  }
  </code></pre>
  
  <p>Is there a way in which i can stop the thread executing the infinite loop?</p>