title: How to interrupt a Thread and catch interruption in the Runnable object
body: <p>I have couple of objects which implement the <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Runnable.html" rel="nofollow noreferrer">Runnable</a> interface and I execute them in separate <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html" rel="nofollow noreferrer">Threads</a>. Essentially in the <code>run()</code> method of the Runnable object I do some network activities which includes call to methods that block while waiting for input (from the network). Note that I do not have any deliberate pauses i.e. <code>Thread.sleep()</code> calls. Any pause is caused by calls to methods that may block.</p>
  <p>These Runnable objects are under the control of a GUI and hence the GUI interface and one function I wish to provide to the user is the ability to end the thread executing my Runnable objects however I'm not able to understand how to do this.</p>
  <p>One obvious means is to call the Thread.interrupt() method of the Runnable objects Thread but how is this call to the Thread method propagated through to the Runnable object? For example I cannot use try-catch, catching <code>InterruptedException</code> in the Runnable object does not seem to be allowed; my IDE (netbeans) complains that <code>InterruptedException</code> is never thrown in the <code>run()</code> method.</p>
  <p>My code is below, stripped for brevity.
  The following lines are executed in the GUI code in the GUI thread:</p>
  <pre><code>digiSearch = new DigiSearch(hostIP,this);
  digiSearchThread = new Thread(digiSearch);
  digiSearchThread.start();
  </code></pre>
  <p>The following is my Runnable class and where I would like/need to capture the interruption of its executing thread.</p>
  <pre><code>public class DigiSearch implements Runnable {
  
  private String networkAdapterIP;
  private DigiList digiList;
  
  public DigiSearch (String ipAddress, DigiList digiList){
      networkAdapterIP = ipAddress;
      this.digiList = digiList;
  }
  @Override
  public void run() {
      
      try{
          /*
   * Do some network and other activities here which includes calling some blocking methods.  However I would like to interrupt the run() method if possible by calling Thread.interrupt()
  */
                      
              
      }  catch (Exception ex){
         digiList.digiListException(ex);
      } catch (Throwable t){
         System.out.println(&quot;Search thread interrupted&quot;);
      }
  }
  </code></pre>
  <p>}</p>
  <p>Could someone enlighten me on how I can achieve this or perhaps resolve my misunderstanding of interrupting threads?</p>