本文共 1155 字,大约阅读时间需要 3 分钟。
当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(); } } } }
转载地址:http://nmdmi.baihongyu.com/