Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Don't you want to call</p> <pre><code>executor.shutdownNow() </code></pre> <p>that will attempt to cancel currently running tasks (using <code>Thread.interrupt</code> so you'll need to implement an 'interruption policy' in each task that uses the interrupt flag).</p> <p>from the javadoc</p> <blockquote> <p>Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.</p> <p>There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt, so any task that fails to respond to interrupts may never terminate.</p> </blockquote> <p>This will return a list of waiting tasks, so you can always put this back onto a 'wait list' rather than loose them completely. You might also want follow that up with an 'await termination' to avoid run away code. For example, <code>executor.awaitTermination(...)</code>.</p> <p><a href="http://tempusfugitlibrary.org/" rel="nofollow">tempus-fugit</a> has some handy classes for handling this. You just call </p> <pre><code>shutdown(executor).waitingForShutdown(millis(400)); </code></pre> <p>see <a href="https://github.com/tobyweston/tempus-fugit/blob/master/src/main/java/com/google/code/tempusfugit/concurrency/ExecutorServiceShutdown.java" rel="nofollow">here</a> for details.</p> <p>Also, the solution you outline in the blog post; I'm not sure if that's quite right. <code>Future.cancel</code> will only stop the task from being scheduled. If you were to update the example in the blog to allow interruption (ie <code>cancel(true)</code>, it'd be equivalent (more or less) with the <code>shutdownNow</code>. That is to say, it will call <code>interrupt</code> on the underlying task which (if you've implemented an interruption policy) will stop it processing. As for cleaning up after interruption, you just need to make sure that you handle that appropriately within the interruption policy implementation. The upshot is that I think you can cancel <em>and</em> cleanup correctly using shutdownNow (or cancel(true))</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload