Servlet“已启动线程但未能停止它” – Tomcat中的内存泄漏

Apache Tomcat多次说过:

Web应用程序[/ MyServlet]似乎已启动名为[pool-61-thread-2]的线程,但未能停止它。这很可能造成内存泄漏。

这很危险吗?该Servlet应该每天能够处理10.000个请求。完成后如何关闭线程?

class Worker {

private final CountDownLatch startSignal;

private final CountDownLatch doneSignal;

private final int threadNumber;

Worker(

CountDownLatch startSignal,

CountDownLatch doneSignal,

int threadNumber

){

this.startSignal = startSignal;

this.doneSignal = doneSignal;

this.threadNumber = threadNumber;

}

public String[][] getSomeStrArrArr() {

String[][] isRs = new String[8][20];

String[][] inRs = new String[8][20];

String[][] iwRs = new String[8][20];

try {

startSignal.await();

if (threadNumber == 1) {

// get String[][] result for thread number 1

isRs = getIS(erg1, erg2, request);

}

if (threadNumber == 2) {

// get String[][] result for thread number 2

inRs = getIN(search_plz, request);

}

if (threadNumber == 3) {

// get String[][] result for thread number 3

iwRs = getIW(erg1, erg2, request);

}

doneSignal.countDown();

} catch (InterruptedException ex) {

System.out.println(

"Thread number "+threadNumber+" has been interrupted."

);

}

if (threadNumber == 1) {

return isRs;

}

if (threadNumber == 2) {

return inRs;

}

if (threadNumber == 3) {

return iwRs;

}

return null;

}

public Callable<String[][]> getSomeCallableStrArrArr(){

return new Callable<String[][]>() {

public String[][] call() throws Exception {

return getSomeStrArrArr();

}

};

}

}

ExecutorService pool = Executors.newFixedThreadPool(3);

Set<Future<String[][]>> set = new HashSet<Future<String[][]>>();

CountDownLatch startSignal = new CountDownLatch(1);

CountDownLatch doneSignal = new CountDownLatch(3);

for (int i=1;i<=3;i++) {

Worker worker = new Worker(startSignal,doneSignal,i);

Callable<String[][]> callable =

worker.getSomeCallableStrArrArr();

Future<String[][]> future = pool.submit(callable);

set.add(future);

}

startSignal.countDown();

try {

doneSignal.await();

回答:

是的,这是一个问题。如果您的代码启动非守护程序线程,则这些线程将继续工作,直到退出运行方法。即使其他一切都完成了,当这些线程继续运行时,旧的JVM仍会徘徊。如果启动新实例,则可能会出现旧线程仍在与新实例创建的线程一起工作的情况。

必须对任务进行设计,以使它们对中断做出响应(这与吃异常并继续进行操作相反,这是您的示例所显示的)。这意味着检查当前线程上的中断标志,并以一种有用的方式捕获InterruptedException,该异常允许任务中断其工作并在需要时重置中断标志。ExecutorService实现具有shutdownNow方法,该方法将中断当前任务。

以上是 Servlet“已启动线程但未能停止它” – Tomcat中的内存泄漏 的全部内容, 来源链接: utcz.com/qa/428928.html

回到顶部