i will not go on implementing the Thread pool itself, but on your question:
Correct way to create task threads in tomcat at startup
as others said, your third approach is almost correct BUT this depends on your Service Structure.
i'll give you an example and then explain it:
public class YourServletContextListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
@Override
public void contextInitialized(ServletContextEvent sce) {
}
}
From the Docs:
contextInitialized:
All ServletContextListeners are notified of context initialization
before any filter or servlet in the web application is initialized.
if i understand your approach correctly, then i would ask you: how would you like to start a service or send any request to a servlet that is still not been initialized?
this would work, if Services you would like to start do not communicate/need any servlet or Filter or any Data from the web App directly. later, after startup the container, they can sure communicate with each other.
as i said before, which way is the best to use depends on the Service Structure/Logic.
One other method may be using a filter:
void init(FilterConfig filterConfig) throws ServletException
Called by the web container to indicate to a filter that it is being
placed into service. The servlet container calls the init method
exactly once after instantiating the filter.
void destroy()
Called by the web container to indicate to a filter that it is being
taken out of service. This method is only called once all threads
within the filter's doFilter method have exited or after a timeout
period has passed. After the web container calls this method, it will
not call the doFilter method again on this instance of the filter.
This method gives the filter an opportunity to clean up any resources
that are being held (for example, memory, file handles, threads) and
make sure that any persistent state is synchronized with the filter's
current state in memory.
but a filter is not designed for such approachs!
Use a Filter if you want to intercept on HTTP requests maching a
specific URL pattern because you want to check/modify the HTTP
request/response. Use a ServletContextListener if you want to
intercept on webapp's startup and/or shutdown.