0

I have a webapp which uses a jar. The jar creates a threadpool. Whenever a request comes in to my servlet I call a method defined in the jar. This method uses one thread from the threadpool maintained by the jar for doing it's work.

The threadpool which the jar uses gets it's threads from where? Is it borrowing the threads from the servlet conainters threadpool?

Manav
  • 259
  • 6
  • 20
  • That depends on the library. It can be creating the `Thread` instances itself or using those provided by an `ExecutorService`. – Sotirios Delimanolis Aug 28 '13 at 18:57
  • it uses java ExecutorService – Manav Aug 28 '13 at 18:58
  • Then the `ExecutorService` is providing the threads. – Sotirios Delimanolis Aug 28 '13 at 18:59
  • say it's not a whole library but just a java class which has a ExecutorService threadpool. Then what happens? isn't the servlet supposed to be taking care of all the threads? – Manav Aug 28 '13 at 18:59
  • Not necessarily. Read [this](http://stackoverflow.com/questions/3745905/what-is-recommended-way-for-spawning-threads-from-a-servlet-in-tomcat) link. – Sotirios Delimanolis Aug 28 '13 at 19:03
  • but if these threads are not managed by the server then why can't the server shutdown and why is it recommended to make these threads daemon threads??? – Manav Aug 28 '13 at 19:09
  • A Java application will only shutdown if no non-daemon threads are running. That's why it's recommended. However, there is no hook for the servlet container to give you threads so you need to manage it yourself (possibly with an `ExecutorService`). – Sotirios Delimanolis Aug 28 '13 at 19:11

0 Answers0