title: How can I wrap a method so that I can kill its execution if it exceeds a specified timeout?
body: <p>I have a method that I would like to call.  However, I'm looking for a clean, simple way to kill it or force it to return if it is taking too long to execute.</p>
  
  <p>I'm using Java.</p>
  
  <p>to illustrate:</p>
  
  <pre><code>logger.info("sequentially executing all batches...");
  for (TestExecutor executor : builder.getExecutors()) {
  logger.info("executing batch...");
  executor.execute();
  }
  </code></pre>
  
  <p>I figure the <code>TestExecutor</code> class should <code>implement Callable</code> and continue in that direction.</p>
  
  <p>But all i want to be able to do is stop <code>executor.execute()</code> if it's taking too long.</p>
  
  <p>Suggestions...?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Many of the suggestions received assume that the method being executed that takes a long time contains some kind of loop and that a variable could periodically be checked.
  However, this is not the case.  So something that won't necessarily be clean and that will just stop the execution whereever it is is acceptable.</p>