title: java - Check if a thread is interrupted without a loop or Thread.sleep()
body: <p>I need to call an external method which sometimes takes indefinite time to complete. </p>
  
  <pre><code>void doMyStuff(){
    // do my stuff
    // work is being done by doStuff thread
    externalMethod(); // This is external and sometimes takes hours
    // I need to check if current thread is interrupted and then return after a certain time 
  }
  </code></pre>
  
  <p>However, I can't afford to keep a thread engaged, so I need to terminate the process after a fixed time. I can interrupt the doStuff thread from an external thread with the help of <a href="https://www.ibm.com/support/knowledgecenter/SSAW57_8.0.0/com.ibm.websphere.javadoc.doc/web/apidocs/com/ibm/websphere/asynchbeans/WorkManager.html" rel="nofollow">Websphere's WorkManager API</a>. But only sending the interrupt signal is useless unless the interrupted thread handles it. </p>
  
  <p>I want to return the doStuff thread to thread pool after a certain time no matter whether the <code>extenalMethod()</code> returns or not. How can this be done?</p>