title: Interrupting a Runnable with a method of the class
body: <p>How can I write a method of a class implementing the <code>Runnable</code> interface (started from an <code>ExecutorService</code>) that would interrupt the thread?</p>
  
  <p>I know that I can use <code>Future fut = es.submit(foo);</code> (where <code>es</code> is the <code>ExecutorService</code> and <code>foo</code> is an instance of my class) and then call <code>fut.cancel(true);</code> from inside the main function of the application, but how do I call something like that from inside the class itself?</p>
  
  <p>I would like to have in my Runnable class a method</p>
  
  <pre><code>public void stop() {…}
  </code></pre>
  
  <p>so that I can call <code>foo.stop()</code> from anywhere (including from the main function of the application) and it will work as if I had used the <code>Future.cancel</code> function (it has to raise ÌnterruptedException` if the thread is awaiting to acquire a Semaphore).      </p>