蓝晶灵 发表于 2017-1-23 08:05:34

Tomcat的ThreadLocalLeakPreventionListener工作原理

  当context stop的时候,如果thread pool的thread没有正常停止的话,而且ThreadLocal中引用了webclassloader加载的对象,有很有可能造成内存泄露,一个解决办法就是杀掉所有的threadpool的线程。Tomcat的ThreadLocalLeakPreventionListener就是这样一个原理 Engine engine = (Engine) context.getParent().getParent();

      Service service = engine.getService();
Connector[] connectors = service.findConnectors();
if (connectors != null) {
for (Connector connector : connectors) {
ProtocolHandler handler = connector.getProtocolHandler();
Executor executor = null;
if (handler != null) {
executor = handler.getExecutor();
}
if (executor instanceof ThreadPoolExecutor) {
ThreadPoolExecutor threadPoolExecutor =
(ThreadPoolExecutor) executor;
threadPoolExecutor.contextStopping();
} else if (executor instanceof StandardThreadExecutor) {
StandardThreadExecutor stdThreadExecutor =
(StandardThreadExecutor) executor;
stdThreadExecutor.contextStopping();
}
}
}
}
 
页: [1]
查看完整版本: Tomcat的ThreadLocalLeakPreventionListener工作原理