如何使用 ps -axl 查找在 Linux 上运行的 Java 线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9934517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:29:48  来源:igfitidea点击:

How to find a Java thread running on Linux with ps -axl?

javalinuxmultithreading

提问by JohnPristine

I have a running JVM with two threads. Is it possible to see these running threads on my Linux OS with ps -axl ? I am trying to find out what priority the OS is giving to my threads. More info about this other issue here.

我有一个带有两个线程的正在运行的 JVM。是否可以使用 ps -axl 在我的 Linux 操作系统上看到这些正在运行的线程?我试图找出操作系统给我的线程的优先级。关于这个其他问题的更多信息在这里

采纳答案by Petro Semeniuk

Use

jps -v

for finding your java process. Sample Output:

用于查找您的 java 进程。示例输出:

3825 RemoteMavenServer -Djava.awt.headless=true -Xmx512m -Dfile.encoding=MacRoman
6172 AppMain -Didea.launcher.port=7533 -Didea.launcher.bin.path=/Applications/IntelliJ IDEA 10.app/bin -Dfile.encoding=UTF-8
6175 Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-411.jdk/Contents/Home -Xms8m

Then use

然后使用

jstack 6172

(6172 is id of your process) to get stack of threads inside jvm. Thread priority could be found from it. Sample output:

(6172 是您的进程的 id)以获取 jvm 中的线程堆栈。可以从中找到线程优先级。示例输出:

.....
"main" **prio=5** tid=7ff255800800 nid=0x104bec000 waiting on condition [104beb000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at au.com.byr.Sample.main(Sample.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

..... 

Enjoy!

享受!

EDIT:If application running under different user than yourself (typical case on production and other non-local environments) then jps/jstack should be run via sudo. Examples:

编辑:如果应用程序在与您不同的用户下运行(生​​产和其他非本地环境的典型情况),则应通过 sudo 运行 jps/jstack。例子:

sudo jps -v

sudo jstack 6172

回答by Wyzard

On Linux, the Sun/Oracle JVM implements Java threads using native Linux threads, so yes, you can see them in "ps" output. Any thread belonging to a javaprocess is a Java thread. But you won't see the threads' names, since those are specific to Java and the OS doesn't know about them.

在 Linux 上,Sun/Oracle JVM 使用本地 Linux 线程实现 Java 线程,所以是的,您可以在“ps”输出中看到它们。属于java进程的任何线程都是 Java 线程。但是您不会看到线程的名称,因为这些名称特定于 Java,而操作系统并不知道它们。

Linux threads do have IDs, but they're just numbers, and "ps axl" doesn't show them. "ps -eLf" does, in the "LWP" column. ("LWP" is short for "lightweight process", which is another name for a thread.) Within Java, the Thread.getId()method mightreturn the LWP number that you see in "ps -eLf" output, but I'm not sure.

Linux 线程确实有 ID,但它们只是数字,“ps axl”不会显示它们。“ps -eLf”在“LWP”列中。(“LWP”是“轻量级进程”的缩写,它是线程的另一个名称。)在 Java 中,该Thread.getId()方法可能会返回您在“ps -eLf”输出中看到的 LWP 编号,但我不确定。

回答by sarnold

The ps(1)thread-selector switch Hwill ask ps(1)to show threads as processes. (Which is roughly how they're implemented anyway.)

ps(1)线程选择器开关H会问ps(1),显示线程的进程。(无论如何,这大致是它们的实现方式。)

See:

看:

$ ps axl | wc -l
163
$ ps axlH | wc -l
325

Apparently I've got a lot of threaded processes running right now.

显然我现在有很多线程正在运行。

回答by Java42

You can make a JNI jump into native code to obtain the native TID associated with the particular Java thread. Then use OS commands or procfs as others have suggested or even better send the particulars of the thread back up to Java.

您可以使 JNI 跳转到本机代码以获取与特定 Java 线程关联的本机 TID。然后按照其他人的建议使用操作系统命令或 procfs,或者更好地将线程的详细信息发送回 Java。

Example: native code

示例:本机代码

JNIEXPORT jlong JNICALL Java_nativeGetThreadId(JNIEnv * env, jobject obj) {
    jlong threadId;
    threadId = (jlong)gettid();
    return threadId;
}

回答by Jayan

If you have JDK installed, you can use a tool called jvisualvmto see the threads (and do many others operations relavant to a java process - see memory, quick check on objects etc)

如果您安装了 JDK,您可以使用名为jvisualvm的工具来查看线程(并执行与 Java 进程相关的许多其他操作 - 查看内存、快速检查对象等)

回答by vasste

  1. find threads ID by top with option H "on", and write down threads PIDs
  2. make thread dump and find the stack of your thread. There are PIDs in hex format.
  1. 使用选项 H "on" 通过 top 查找线程 ID,并记下线程 PID
  2. 进行线程转储并找到线程的堆栈。有十六进制格式的PID。

回答by rkrakz

All of the methods mentioned here work just fine. I was also searching for something similar and came across this blogby Timur Akhmadeev. I hope it helps.

这里提到的所有方法都可以正常工作。我也在寻找类似的东西,并 偶然发现了 Timur Akhmadeev 的这个博客。我希望它有帮助。

Edit:

编辑:

As I was pointed by fellow programmers, the following is a summary of the post by Timur:

正如其他程序员所指出的,以下是 Timur 帖子的摘要:

On a *nux based system first do a

在基于 *nux 的系统上首先做一个

top -H

to show you CPU usage on a per-thread basis. Look for user as oracle and in the command as Java. Note the PID for this process and then run

在每个线程的基础上向您显示 CPU 使用率。查找用户为 oracle,在命令中查找为 Java。记下此进程的 PID,然后运行

top -H -p PID

This brings up a list that displays all the tasks that the process(java program) is currently performing. After this we need to know what task each thread may be performing, for which we use a utility that the jdk provides, namely jstack. In linux, Java (JVM HotSpot) threads are mapped to threads that of the kerner.

这将打开一个列表,显示进程(java 程序)当前正在执行的所有任务。在此之后,我们需要知道每个线程可能正在执行什么任务,为此我们使用 jdk 提供的实用程序,即 jstack。在 linux 中,Java (JVM HotSpot) 线程被映射到内核线程。

jstack -pid_of_the_thread

To map OS level thread to a Java thread in a thread dump, we need to convert native thread ID from Linux to base 16, and search for “nid=$ID” in the stack trace. For example, thread ID is 7601 is 0x1db1 may be one you were monitoring. If you would like to further keep an eye out for what the thread may do in the future, i.e, track its behavior, simply write a shell script to watch for changes. Hope this makes sense.

要将操作系统级线程映射到线程转储中的 Java 线程,我们需要将本地线程 ID 从 Linux 转换为基数 16,并在堆栈跟踪中搜索“nid=$ID”。例如,线程 ID 是 7601 是 0x1db1 可能是您正在监视的一个。如果您想进一步关注线程在未来可能会做什么,即跟踪其行为,只需编写一个 shell 脚本来观察变化。希望这是有道理的。