title: How to properly kill local threads owned by a webapp running on tomcat instructed to shutdown
body: <p>A backend webapp is deployed on a Tomcat 6 servlet container. In the webapp, several monitoring threads are started.  The problem is with shutdown.</p>
  
  <ul>
  <li>How do I know that the webapp is requested to shutdown?</li>
  <li>How should I handle this in my threads?</li>
  </ul>
  
  <p>Currently my thread is implemented as below. When the servlet is instructed to shutdown (shutdown.sh) it does complete a clean shutdown and does not hang because of this thread -- Why?</p>
  
  <pre><code>class Updater extends Thread {
    volatile boolean interrupted = false;
  
    @Override
    public void run() {
      Integer lastUpdateLogId = CommonBeanFactory.getXXX()
                                .getLastUpdateLogRecordKey(MLConstants.SMART_DB_NAME);
  
      List&lt;UpdateLog&gt; updateLogRecords;
      while (!interrupted) {
        boolean isConfigurationUpdateRequested = false;
  
        try {
          TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
          setInterrupted(true);
        }
  
        updateLogRecords = CommonBeanFactory.getXXX()
                           .getLastFactsUpdateLogRecords(MLConstants.XXXX, lastUpdateLogId);
  
        for(UpdateLog updateLog : updateLogRecords) {
          if (updateLog.getTable_name().equals(MLConstants.CONFIG_RELOAD)) {
            isConfigurationUpdateRequested = true;
          }
  
          lastUpdateLogId = updateLog.getObjectKey();
        }
  
        if (isConfigurationUpdateRequested) {
          Configuration.getInstance().loadConfiguration();
        }
      }
    }
  
    public boolean getInterrupted() {
      return interrupted;
    }
  
    public void setInterrupted(boolean interrupted) {
      this.interrupted = interrupted;
    }
  }
  </code></pre>