Reputation: 5622
I have web application created and running on Jetty. I have used the ExecutorService to create
a new Threadpool like this,
ExecutorService es = Executors.newFixedThreadPool(10);
But I heard creating your own thread pool in a application server is not advisable, and I must let the application server handle the threads.
So how can I do that in Jetty.
Pardon me I'm new to Jetty and Thanks in advance
Here is the program,
public class SampleExecutors {
public SampleExecutors() {
ExecutorService executors = Executors.newFixedThreadPool(10);
Callable<List<Map<Record,String>>> text = new TextSearcher(domain, searchText);
Future<List<Map<Record,String>>> submit = executors.submit(text);
executors.shutdown();
}
}
log.info("Active threads count:"+Thread.activeCount());
Here, after the created worker threads complete its task, I'm calling the executors.shutdown()
but still, if I do a Thread.activeThreads(), I can see the created threads.
So what happens is, as the instatiation is in the constructor and its not a singelton, for every time SampleExecutors is called, a new threadpool is created.
I dont want to make the Executors static or move it from the constructor, I want to know how to close or kill the created threads after they serve the task.
Upvotes: 1
Views: 1122
Reputation: 4974
Afaik jetty is not an AS but a simple web server with a servlet container. Writing multithreaded code in a servlet container is ok.
Futher reading:
Difference between each instance of servlet and each thread of servlet in servlets?
http://balusc.blogspot.co.uk/2010/06/servlet-lifecycle-and-multithreading.html
Upvotes: 1