gpt4 book ai didi

java - tomcat:WAITING条件线程

转载 作者:行者123 更新时间:2023-11-28 22:01:16 26 4
gpt4 key购买 nike

我正在调查 tomcat 关闭进程的奇怪问题:在运行 shutdown.sh 之后,java 进程仍然出现(我使用 ps -ef|grep tomcat 检查它)情况有点复杂,因为我对服务器的访问权限非常有限(例如,没有调试)

我使用线程转储(通过使用 kill -3 <PID> )和堆转储使用远程 jConsole和热点功能。

在查看线程转储后,我发现了这个:

"pool-2-thread-1" #74 prio=5 os_prio=0 tid=0x00007f3354359800 nid=0x7b46 waiting on condition [0x00007f333e55d000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000c378d330> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1081)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)

所以,我对问题的理解如下:有一个资源(数据库连接或其他)在 CachedThreadpool 中使用,现在这个资源被锁定,并防止线程pool-2-thread-1停止。假设这个线程不是守护进程——JVM 不能优雅地停止。

有没有办法找出哪个资源被锁定,从哪里锁定以及如何避免这种情况?另一个问题是——如何防止这种情况发生?另一个是:地址 0x00007f333e55d000 是做什么用的?

谢谢!

最佳答案

经过一阵挣扎我发现,卡住线程总是有相同的名称pool-2-thread-1。我 grep 项目的所有源代码以查找启动任何计划线程池的任何位置:Executors#newScheduledThreadPool。经过大量时间后,我记录了所有这些地方,线程池中的线程名称。再重启一次服务器,就搞定了!

我找到了一个线程池,它只用一个线程启动并使用了以下代码:

private void shutdownThreadpool(int tries) {
try {
boolean terminated;
LOGGER.debug("Trying to shutdown thread pool in {} tries", tries);
pool.shutdown();
do {
terminated = pool.awaitTermination(WAIT_ON_SHUTDOWN,TimeUnit.MILLISECONDS);
if (--tries == 0
&& !terminated) {
LOGGER.debug("After 10 attempts couldn't shutdown thread pool, force shutdown");
pool.shutdownNow();
terminated = pool.awaitTermination(WAIT_ON_SHUTDOWN, TimeUnit.MILLISECONDS);
if (!terminated) {
LOGGER.debug("Cannot stop thread pool even with force");
LOGGER.trace("Some of the workers doesn't react to Interruption event properly");
terminated = true;
}
} else {
LOGGER.info("After {} attempts doesn't stop", tries);
}
} while (!terminated);
LOGGER.debug("Successfully stop thread pool");
} catch (final InterruptedException ie) {
LOGGER.warn("Thread pool shutdown interrupted");
}
}

之后问题就解决了。

关于java - tomcat:WAITING条件线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45196851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com