title: Thread pool with ServletContextListener
body: <p>I want to run some simple background process calculations but I can't seem to figure it out. No matter what I do, it blocks.</p>
  
  <pre><code>public class WorkThreadManagerContextLoaderListener implements ServletContextListener {
      private Runnable runnable;
      private WorkManager workManager;
  
      @Override
      public void contextInitialized(ServletContextEvent event) {
          final WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
          workManager = (WorkManager) springContext.getBean("workThreadManager");
  
          runnable = new WorkThreadManagerStartUp(WorkManager);
  
          runnable.run();
      }
  
      @Override
      public void contextDestroyed(ServletContextEvent event) {
          workManager.shutDown();
      }
  }
  </code></pre>
  
  <p>The reason why WorkThreadManagerStartUp exist is because I don't want it to block so I made it a Runnable type and when run() is called, it starts up an ExecutorService:</p>
  
  <pre><code>public class UnitOfWorkThreadManagerStartUp implements Runnable {
      private WorkManager workManager;
  
      public UnitOfWorkThreadManagerStartUp(WorkManager workManager) {
          this.workManager = workManager;
      }
  
      @Override
      public void run() {
          workManager.startUp();
      }
  }
  
  public class WorkThreadManager implements WorkManager {
      @Autowired
      private WorkService workService;
  
      private final int availableProcessors = Runtime.getRuntime().availableProcessors();
      private final ExecutorService executorService = Executors.newFixedThreadPool(4 * availableProcessors);
  
      @Override
      public void startUp() {
          // this method always blocks...
      }
  }
  </code></pre>
  
  <p>But my solution doesn't work as expected. I'm running on Tomcat 7.0.30.</p>
  
  <p>What I'm trying to figure out is how I can start a thread pool in the background without stopping the web app from deploying because currently it can never fully come online due to startUp() always blocking. I'd like to simplify this solution and possibly removing WorkThreadManagerStartUp class if it's not really needed.</p>
  
  <p>EDIT:</p>
  
  <p>I modified the start up class</p>
  
  <pre><code>public class WorkThreadManagerStartUp implements Runnable {
      private WorkManager workManager;
  
      public WorkThreadManagerStartUp(WorkManager workManager) {
          this.workManager = workManager;
      }
  
      @Override
      public void run() {
      try {
          while (true) {
                  System.out.println("Hello World!");
                  Thread.sleep(1000 * 10);
              }
          } catch(InterruptedException e) {
              System.out.println(e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>Although this is suppose to run on it's own thread, Hello World! displays multiple times as expected but does not allow the web app to come online.</p>