我如何弄清楚什么是锁定并阻止我的Java线程?

时间:2021-03-23 03:59:34

I'm using Wildfly 11 with Java 8 on Amazon Linux. We're troubleshooting a high CPU utilization issue and have the Wildfly CLI tool at our disposal. I noticed some of our Java threads have been running for quite awhile and wanted to figure out why. I found the ID of one thread and checked its status ...

我在Amazon Linux上使用Wildfly 11和Java 8。我们正在解决高CPU利用率问题,并且可以使用Wildfly CLI工具。我注意到我们的一些Java线程已经运行了很长一段时间,并想弄明白为什么。我找到了一个线程的ID并检查了它的状态......

[standalone@localhost:9990 /] /core-service=platform-mbean/type=threading/:get-thread-infos (ids=[2L])
{
    "outcome" => "success",
    "result" => [{
        "thread-id" => 2L,
        "thread-name" => "Reference Handler",
        "thread-state" => "WAITING",
        "blocked-time" => -1L,
        "blocked-count" => 48628L,
        "waited-time" => -1L,
        "waited-count" => 44877L,
        "lock-info" => {
            "class-name" => "java.lang.ref.Reference$Lock",
            "identity-hash-code" => 926576350
        },
        "lock-name" => "java.lang.ref.Reference$Lock@373a6ede",
        "lock-owner-id" => -1L,
        "lock-owner-name" => undefined,
        "stack-trace" => [],
        "suspended" => false,
        "in-native" => false,
        "locked-monitors" => [],
        "locked-synchronizers" => []
    }]
}

I don't have a firm understanding of everything here but it does seem like one issue is a lock -- "class-name" => "java.lang.ref.Reference$Lock" . Does anyone know how I can get more information about what's causing the lock?

我对这里的所有内容都没有一个明确的理解,但似乎有一个问题是锁 - “class-name”=>“java.lang.ref.Reference $ Lock”。有谁知道如何获得有关导致锁定的更多信息?

1 个解决方案

#1


2  

Can you get to the box using ssh?

你能用ssh进入盒子吗?

If you are running Java process with JDK (not JRE) that you can check the status of the threads using jstack utility.

如果您使用JDK(而不是JRE)运行Java进程,则可以使用jstack实用程序检查线程的状态。

If yes, you can get thread dump immediately of all the threads of the process using:

如果是,您可以使用以下方法立即获取进程所有线程的线程转储:

jstack <process_id>

You can save into a file using

您可以使用保存到文件中

jstack <process_id> > <file_name>

Then you can check state of the threads and thread locks in the file_name. You may figure out what threads are blocked and what locks they are holding.

然后,您可以检查file_name中的线程和线程锁的状态。您可以弄清楚哪些线程被阻塞以及它们持有什么锁。

jstack command doesn't stop the process. It's safe to run it on a running process.

jstack命令不会停止进程。在正在运行的进程上运行它是安全的。

At least I will start with this simple approach for any blocked Java application.

对于任何被阻止的Java应用程序,至少我将从这个简单的方法开始。

#1


2  

Can you get to the box using ssh?

你能用ssh进入盒子吗?

If you are running Java process with JDK (not JRE) that you can check the status of the threads using jstack utility.

如果您使用JDK(而不是JRE)运行Java进程,则可以使用jstack实用程序检查线程的状态。

If yes, you can get thread dump immediately of all the threads of the process using:

如果是,您可以使用以下方法立即获取进程所有线程的线程转储:

jstack <process_id>

You can save into a file using

您可以使用保存到文件中

jstack <process_id> > <file_name>

Then you can check state of the threads and thread locks in the file_name. You may figure out what threads are blocked and what locks they are holding.

然后,您可以检查file_name中的线程和线程锁的状态。您可以弄清楚哪些线程被阻塞以及它们持有什么锁。

jstack command doesn't stop the process. It's safe to run it on a running process.

jstack命令不会停止进程。在正在运行的进程上运行它是安全的。

At least I will start with this simple approach for any blocked Java application.

对于任何被阻止的Java应用程序,至少我将从这个简单的方法开始。