title: Java long running task Thread interrupt vs cancel flag
body: <p>I have a long running task, something like:</p>
  
  <pre><code>public void myCancellableTask() {
      while ( someCondition ) {
         checkIfCancelRequested();
         doSomeWork();
      }
   }
  </code></pre>
  
  <p>The task can be cancelled (a cancel is requested and checkIfCancelRequested() checks the cancel flag). Generally when I write cancellable loops like this, I use a flag to indicate that a cancel has been requested. But, I know I could also use Thread.interrupt and check if the thread has been interrupted. I'm not sure which would be the preferred approach and why, thoughts?</p>
  
  <p>thanks,</p>
  
  <p>Jeff</p>