Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A scheduled task is either being executed or is waiting to be executed. </p> <p>If the task is waiting to be executed, <code>future.cancel()</code> will prevent it from being executed (both cancel(true)/cancel(false)). </p> <p>If the task is already being executed, <code>future.cancel(false)</code> will have no effect. <code>future.cancel(true)</code> will interrupt the thread that is executing that task. <strong>Whether this will have any effect is up to you</strong>, who will implement that task. A task may or may not respond to interruption depending on the implementation. </p> <p>In order to make your task responsive to cancellation, you must implement <code>doSomething()</code> so that it will respond to interruption. </p> <p>There are basically two way to do this:</p> <p>1.Check interruption flag in your logic</p> <pre><code>public void doSomething(){ stuff(); //Don't use Thread.interrupt() if(Thread.currentThread().isInterrupted()){ // We have an interruption request, possibly a cancel request //Stop doing what you are doing and terminate. return; } doLongRunningStuff(); } </code></pre> <p>You must occasionally check for the interruption flag, and if interrupted, stop what you are doing and return. Be sure to use Thread.isInterrupted() and not Thread.interrupt() for the check.</p> <p>2.Act upon Interrupted exception</p> <pre><code>public void doSomething(){ try{ stuff(); }catch(InterruptedException e){ // We have an interruption request, possibly a cancel request // First, preserve Interrupted Status because InterruptedException clears the // interrupted flag Thread.currentThread.interrupt(); // Now stop doing your task and terminate return; } doLongRunningStuff(); } </code></pre> <p>When you have any method that throws InterruptedException, be sure to stop what you doing and terminate when one is thrown. </p> <p>Once you implement your methods in this way, you can call future.cancel(true) to cancel the execution of a running task.</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