Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to set allowCoreThreadTimeOut() in ThreadPoolExecutor?

In our application, we have a lot of ThreadPoolExecutors. When the application is idle, the ThreadPoolExecutors are also idle, but the number of idle threads in the application is very high.

In the thread dump, I found out that most of the threads belong to the ThreadPoolExecutors waiting for tasks. Are there any side effects in keeping these worker threads alive? Should I use setAllowCoreThreadTimeOut() in ThreadPoolExecutor so that the worker threads die after being idle for sometime?

like image 596
prasanth Avatar asked Sep 29 '22 22:09

prasanth


1 Answers

Every thread has an associated stack which requires some amount of memory (configurable). If your threads are in the waiting state (and even if they aren't), they are reserving that memory. It can't be used by the rest of your application. Therefore, it might make sense to stop those threads (with setAllowCoreThreadTimeout()), release the memory they had reserved, and let the ThreadPoolExecutor recreate them as needed.

like image 148
Sotirios Delimanolis Avatar answered Oct 05 '22 06:10

Sotirios Delimanolis