Whether a java thread pool that is no longer referenced needs to perform shutdown?Does jvm automatically release thread resources?
Asked
Active
Viewed 156 times
0
-
4Your question is unclear. Could you give a short example code that shows what local variable you mean? Please [edit] your question and add the code, and use the `{}` button to format it. – RealSkeptic May 30 '18 at 14:01
1 Answers
2
See the doc:
A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically.
However, by default, the threads in the pool will not terminate automatically, they will keey waiting for new tasks. So the resources will not be released.
If you would like to ensure that unreferenced pools are reclaimed even if users forget to call shutdown(), then you must arrange that unused threads eventually die, by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).
And in my personal experience, I have got OutOfMemory
error because did not call shutdown
or allowCoreThreadTimeOut
.

xingbin
- 27,410
- 9
- 53
- 103