Answering your short question:
In JVM thread pools are abstracted behind java.util.concurrent.ExecutorService
interface. There are different implementations of this interface but in most cases methods of this interface would suffice.
To create specific thread pool, take a look at java.util.concurrent.Executors
class:
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html
which contains static factory methods for creating different implementations of ExecutorService
interface. You may be interested in newFixedThreadPool(int threadsNumber)
and newCachedThreadPool
methods.
For more general information on Executors
in JVM you may want to read following Oracle's tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html
So, to use thread pool (ExecutorService
) under Tomcat you should do the following:
.1. Create and register in web.xml
instance of javax.servlet.ServletContextListener
interface (which would act like an entry point to your webapplication) if it's not done yet.
.2. In contextInitialized(ServletContextEvent)
method, you create instance of ExecutorService
(thread pool) and store it in ServletContext
attribute map, so that it can be accessed from any point in you webapp e.g.:
// following method is invoked one time, when you web application starts (is deployed)
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
// ...
final int numberOfThreads = ...;
final ExecutorService threadPool = Executors.newFixedThreadPool(numberOfThreads); // starts thread pool
final ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.setAttribute("threadPoolAlias", threadPool);
// ...
}
// following method is invoked one time when your web application stops (is undeployed)
public void contextDestroyed(ServletContextEvent servletContextEvent) {
// following code is just to free resources occupied by thread pool when web application is undeployed
final ExecutorService threadPool = (ExecutorService) servletContextEvent.getServletContext().getAttribute("threadPoolAlias");
threadPool.shutdown();
}
.3. Somewhere in Servlet.service
method or anywhere in your webapp (you should be able to obtain reference to ServletContext
almost anywhere from webapp):
Callable<ResultOfMyTask> callable = new Callable<ResultOfMyTask>() {
public ResultOfMyTask call() {
// here goes your task code which is to be invoked by thread pool
}
};
final ServletContext servletContext = ...;
final ExecutorService threadPool = (ExecutorService) servletContext.getAttribute("threadPoolAlias");
final Future<ResultOfMyTask> myTask = threadPool.submit(callable);;
You should store reference to myTask and can query it from other threads to find out whether it's finished and what is the result.
Hope this helps...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…