Use Case: Create a new thread every time I need to process a job.
Current Implementation: I am using Executor Service with fixed size thread pool, of say 50. For every job, I submit a new thread to executor service.
Issue: Once the job is completed, the threads do not die and go into waiting state. (waiting at sun.misc.unsafe.park)
Analysis: As per this link (WAITING at sun.misc.Unsafe.park(Native Method)) and other sources on the net, this is a valid scenario, and the threads go into waiting state, waiting for some task to be given to them.
Question: From Java mission control, I am able to deduce that the threads are not using any resources and are not in dead lock. So that is good. But consider a time frame when lots of jobs are submitted and all 50 threads in the pool were instantiated. And after that all the 50 threads are going to be alive even though the job submission rate might have decreased. I cannot shut down the executor service also, since it needs to be alive forever waiting for jobs to be submitted. If I create, normal threads, I see that the threads die after their job is done. But in that case there is no tab in the max amount of threads being created. So in a peak time, we may run into a situation where more threads are created than what the JVM can handle.
How can this scenario be handled in the best possible way. Should we ignore the threads being in the waiting state or should I go for any other implementation.
The behavior that I am trying to implement is more like autoscaling. Span more servers(threads in this case)during peak time. And terminate the extra servers and maintain a minimum server count when the load is not that high.