1

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.

jamp
  • 2,159
  • 1
  • 17
  • 28
mani_nz
  • 4,522
  • 3
  • 28
  • 37

1 Answers1

1

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

Community
  • 1
  • 1
zeller
  • 4,904
  • 2
  • 22
  • 40
  • Ok.. So you mean it's not mandatory to create a thread pool from server side? The way how I did is also acceptable?? – mani_nz Sep 09 '13 at 18:41
  • I have added few more things in my question about why I feel creating the our own executorService/Threadpool might be incorrect. – mani_nz Sep 12 '13 at 12:10
  • See if the task is really finished. Maybe something is still running. Also read the sources of ThreadPoolExecutor.shutdown. there are some nonjavadoc doc about who can shutdown the workers. And finally try debugging the code if you can. – zeller Sep 12 '13 at 12:29