title: What is happening underneath the Future.cancel(true)
body: <p>Suppose I have a <code>Runnable</code> instance:</p>
  
  <pre><code>class MyTask implements Runnable {
  
    public void run() {
       //some heavy calculation which takes time
        Thread.sleep(5000)
       //rest code
       ...
    }
  
  }
  </code></pre>
  
  <p>Then, I use ExecutorService to submit the above task:</p>
  
  <pre><code>ExecutorService service = Executors.newFixedThreadPool(3);
  Future&lt;?&gt; task = service.submit(new MyTask());
  </code></pre>
  
  <p>Now, I can cancel the task by <code>task.cancel(true);</code>. What I have understood is that the <code>task.cancel(true)</code> will interrupt the working thread in which this task is running, like <code>Thread.currentThread().interrupt()</code>. But this <strong>only sets a flag</strong> to tell that the working thread is interrupted. </p>
  
  <p>My question is: if <code>MyTask</code> Runnable has started running, how actually does <code>future.cancel(true)</code> stops my code in <code>run()</code> continuing executing the rest code? Is there a periodical checking for the working thread's interrupted flag underneath?  I mean I don't understand how the code in run() can be canceled by only set the interrupted flag to true.</p>