简体   繁体   English

如何调试挂起的java线程?

[英]How can I debug a hanging java thread?

I have the following problem: 我有以下问题:
I deploy a web application in Tomcat (Linux) and after shutdown of Tomcat, if I do ps -ef I still can see the java process running. 我在Tomcat(Linux)中部署Web应用程序,在关闭Tomcat之后,如果我执行ps -ef我仍然可以看到java进程正在运行。
I believe this happens due to some hanging thread but I don't know how can I track this thread down. 我相信这是由于一些悬挂线程而发生但我不知道如何跟踪这个线程。
How can I debug this issue? 我该如何调试此问题?

You can generate 4-5 thread dumps as described below and then analyze them using tools like Samurai . 您可以生成4-5个线程转储,如下所述,然后使用Samurai等工具进行分析。

What you want to check is when a stuck thread or long running transaction happens, all the thread dumps will show a certain thread id is at the same line in your java stack trace. 你想要检查的是当一个卡住的线程或长时间运行的事务发生时,所有的线程转储都会显示某个线程id在你的java堆栈跟踪的同一行。 In simpler terms, the transaction is spanning across multiple thread dumps and hence needs more investigation. 简而言之,事务跨越多个线程转储,因此需要更多调查。

Now when you run these through Samurai , it will highlight these in Red colour so you can quickly click on it and get to the lines showing issues. 现在当你通过Samurai运行它们时,它会突出显示红色,这样你就可以快速点击它并找到显示问题的线条。

See an example of this here . 在这里查看此示例。 Look at the Samurai output image in that link. 查看该链接中的Samurai输出图像。 The Green cells are fine. 绿色细胞很好。 Red and Grey cells need looking at. 红色和灰色细胞需要看。

Generating a Thread Dump: 生成线程转储:

(Linux) (Linux)的

If the JVM is running in a console then simply press Ctrl-\\ . 如果JVM在控制台中运行,则只需按Ctrl-\\ If the JVM is running in the background then send it the QUIT signal: 如果JVM在后台运行,则向其发送QUIT信号:

kill -QUIT process_id

There process_id is the process number of the running Java process. process_id是正在运行的Java进程的进程号。 The thread dump will be sent to wherever standard out is redirected too. 线程转储将被发送到重定向标准输出的任何地方。 You can generally get the process numbers of of all running Java processes with the command: 您通常可以使用以下命令获取所有正在运行的Java进程的进程号:

ps axf | grep java

You say your java process still exists, right? 你说你的java进程仍然存在,对吗? Processes exist as long as they have attached threads, right? 只要有连接线程就存在进程,对吧? If so, I would go for the following approach: - run the process with the MBean server attached and managed internally by the JVM 如果是这样,我会采用以下方法: - 在JVM内部附加和管理MBean服务器的情况下运行该进程

Then connect to the process after you send the QUIT signal and get the thread dump (there should be a JMX for that. See which threads look suspicious to you. 然后在发送QUIT信号并获取线程转储后连接到进程(应该有一个JMX。请查看哪些线程看起来可疑。

I think you can also use JVisualVM to take thread dumps... 我想你也可以使用JVisualVM来进行线程转储......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM