title: ThreadPoolTaskExecutor graceful shutdown
body: <p>I'm using the Spring (v4) <code>ThreadPoolTaskExecutor</code> to execute some continuous runnable tasks. When the application shuts down, I want a graceful shutdown of the executor so that tasks have some time to complete their iteration before continuing the shutdown. If an active task completes its iteration prior to the executors wait time expiring (e.g. <code>ThreadPoolTaskExecutor.setAwaitTerminationSeconds()</code>), I do not want it to start another iteration. So far I have not been able to accomplish this. I have the following executor configuration:</p>
  
  <pre><code>ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  executor.setCorePoolSize(5);
  executor.setMaxPoolSize(10);
  executor.setWaitForTasksToCompleteOnShutdown(true);     
  executor.setAwaitTerminationSeconds(30);
  </code></pre>
  
  <p>And my tasks are essentially setup as such:</p>
  
  <pre><code>    myExecutor.execute(() -&gt; {
          while(true) {
              doSomething();
          }
      });
  </code></pre>
  
  <p>I assume I need to set a flag in my thread when the executor shuts down so that the loop breaks. Is there another recommended approach?</p>