Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. COBut the `shutdownNow()` method doesn't call the `Future.cancel()` of the scheduled jobs. Actually, it uses a private `Worker` List to interrupt the threads with the `Thread.interrupt()` method. So, my decorators aren't invoked with the `shutdownNow()` method. In other hand, you're right: the `shutdown()` method will call the `Future.cancel(false)` without interrupting the thread. So what we actually do is to release the resources when the param `mayInterruptIfRunning = true` and we overwrite the `shutdownNow()` to call the `Future.cancel(true)` before calling `super.shutdownNow()`.
      singulars
    2. COThe question is: Which jobs list I must use in my custom `shutdownNow()` method to force correctly all the jobs? If I use the `getQueue()` method I will get the scheduled and periodicals ones (and this list may be empty if `shutdown()` is called before). But what about the thread that are actually running? The `shutdownNow()` will interrupt them, but how can I force them to release theirs resources? I need a reference to them in order to call the `Future.cancel(true)` and for that I keep a list as suggested in other answer. I don't know if this is the best way to do it
      singulars
    3. COI guess I'm saying why call cancel at all? What's the difference you see here than with interrupt? Ie. if you can release resources on interrupt, you may not need a custom cancel. I think exploring *not* using cancel more might be worth while...
      singulars
 

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