title: how to shutdown ThreadPoolTaskExecutor? Good way
body: <p>I have ThreadPoolTaskExecutor.  I should send too many emails (different emails). If I have error during email sending, I should write it in database.</p>
  
  <pre><code>&lt;bean id="taskExecutor"
      class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt;
      &lt;property name="corePoolSize" value="5" /&gt;
      &lt;property name="maxPoolSize" value="10" /&gt;
      &lt;property name="WaitForTasksToCompleteOnShutdown" value="true" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I'm executing tasks.  taskExecutor.execute(Runable). Everything works well!</p>
  
  <pre><code>@Override
  public void run() {
      try {
          mailService.sendMail();
      } catch (Throwable t) {
          daoService.writeFaillStatus();
      }
  } 
  </code></pre>
  
  <p>Everything seems ok! Async request are doing well! </p>
  
  <p>I also have </p>
  
  <pre><code>  white(true) { 
      if(executor.getActiveCount()==0){
         executor.shutdown(); break;
       }
        Thread.sleep(3000)
      }
  </code></pre>
  
  <p>Because of WaitForTasksToCompleteOnShutdown=true task never will be shut down automatically.  In the other words, main thread never will be destroyed (Main thread is the thread where I invoke thread executor tasks - when I run code in eclipse, terminal is active always). Even after executor thread will finish their work my console looks like this: <img src="https://i.stack.imgur.com/1d47y.jpg" alt="enter image description here"></p>
  
  <p>I think this is because, main thread is waiting something - someone who tell that <strong>"everything is already done, relax, go and shut down"</strong></p>
  
  <p>So thought about this solution <strong>while(true)</strong>. Could you tell me if this is good idea? Might be it is not good.</p>
  
  <p>I know that this executor also have submit() method. I think I do not need here. Please correct me If I am not correct in this post.</p>