2017-07-19 99 views
2

我正在调查与tomcat关机过程有关的奇怪问题:在runnig shutdown.sh之后,java进程仍然出现(我通过使用ps -ef|grep tomcat来检查它) 这种情况有点复杂,因为我有对服务器的访问非常有限(例如,没有调试)tomcat:等待条件线程

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

寻找到线程转储后,我发现这一点:

"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使用的资源(DB连接或别的东西),而这个资源现在被锁定, 并阻止线程pool-2-thread-1停止。假设这个线程不是deamon - JVM不能正常停止。

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

谢谢!

回答

0

经过一番苦苦挣扎之后,我发现冻线总是有相同的名字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"); 
    } 
} 

之后,这个问题得到了解决。