【发布时间】:2017-02-21 15:40:09
【问题描述】:
我有一个 REST 应用程序(堆栈:Java8/Tomcat7/Spring4.2/Jersey2.15)并在我的 REST 端点中使用一个线程池 (ThreadPoolExecutor)。
private ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
端点类配置了spring注解@Component(所以默认是单例)。
在 ThreadPoolExecutor 的 javadoc 中,我读到了这个:
Finalization
A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. 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).
所以当我不调用 threadPool.shutdown() 时,池将在应用程序结束时关闭(通常在这种情况下当 tomcat 关闭或应用程序将停止时)。 p>
我的问题是:我必须关心池的关闭吗?会不会出现副作用? (例如内存泄漏、Tomcat 关闭时间过长……)还是坚如磐石?
【问题讨论】:
标签: java spring multithreading tomcat