获取Linux中Java线程的线程ID [英] Obtaining the thread ID for Java threads in Linux
问题描述
我有一个Java应用程序,其中创建了一些线程(通过new Thread()
).使用ps
,我可以看到它们具有不同的线程ID(LWP列),我想从Java应用程序中获取这些ID.
在我发现的与此主题相关的大多数文章中(例如,此文章),解决方案是使用ManagementFactory.getRuntimeMXBean().getName()
.
但是,使用该方法可以给我主线程的PID(即使我从其中一个线程调用它),因此它并不能真正解决我的问题.
是否有任何方法可以为应用程序创建的每个Thread
获取线程ID?
是否可以使用JNI完成它?如果可以通过某种方式连接到可以调用syscall(__NR_gettid)
的C函数,则可以解决我的问题.我真的不在乎可移植性,因此我对只适用于Linux机器的解决方案完全没问题.
更新:我实际上已经通过使用JNI解决了我的问题.详细信息在我的答案中进行了解释.谢谢大家的建议/评论.
最后,我发现JNI方法是解决我的问题的最佳方法.作为参考,我发布了代码并为其构建了说明(基于Wikipedia上的示例):>
负责连接C代码(GetThreadID.java
)的Java类:
public class GetThreadID {
public static native int get_tid();
static {
System.loadLibrary("GetThreadID");
}
}
负责获取线程ID(GetThread.c
)的C文件:
#include <jni.h>
#include <syscall.h>
#include "GetThreadID.h"
JNIEXPORT jint JNICALL
Java_GetThreadID_get_1tid(JNIEnv *env, jobject obj) {
jint tid = syscall(__NR_gettid);
return tid;
}
如何使用GetThreadID
类的示例:
class Main {
public static void main(String[] args) {
int tid = GetThreadID.get_tid();
System.out.println("TID=" + tid);
}
}
最后,构建指令(javah
自动生成GetThreadID.h
):
JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:bin/javac::")
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
javac GetThreadID.java
javah GetThreadID
gcc -I${JAVA_HOME}/include -fPIC -shared GetThreadID.c -o libGetThreadID.so
javac Main.java
java Main
I have a Java application where some threads are created (via new Thread()
). Using ps
I can see they have different thread IDs (LWP column) and I would like to obtain those IDs from within the Java application.
In most of the posts related to this topic that I have found (e.g., this one), the solution is to use ManagementFactory.getRuntimeMXBean().getName()
.
Using that method, however, gives me the PID of the main thread (even if I call it from one of the threads), so it is not really solving my problem.
Is there any way to obtain the thread ID for every single Thread
created by an application?
Would it be possible to use JNI to accomplish it? If somehow I could interface to a C function where I could call syscall(__NR_gettid)
, that could solve my problem. I really do not care about portability, so I am totally okay with a solution that would only work for a Linux machine.
UPDATE: I have actually solved my problem by using JNI. Details are explained in my answer. Thank you all for your suggestions/comments.
In the end, I found the JNI way to be the best one to solve my problem. As a reference, I post the code and build instructions for it (based on the example at Wikipedia):
Java class responsible to interface to the C code (GetThreadID.java
):
public class GetThreadID {
public static native int get_tid();
static {
System.loadLibrary("GetThreadID");
}
}
C file responsible to obtain the thread ID (GetThread.c
):
#include <jni.h>
#include <syscall.h>
#include "GetThreadID.h"
JNIEXPORT jint JNICALL
Java_GetThreadID_get_1tid(JNIEnv *env, jobject obj) {
jint tid = syscall(__NR_gettid);
return tid;
}
An example of how to use GetThreadID
class:
class Main {
public static void main(String[] args) {
int tid = GetThreadID.get_tid();
System.out.println("TID=" + tid);
}
}
And finally, the build instructions (javah
automatically generates GetThreadID.h
):
JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:bin/javac::")
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
javac GetThreadID.java
javah GetThreadID
gcc -I${JAVA_HOME}/include -fPIC -shared GetThreadID.c -o libGetThreadID.so
javac Main.java
java Main
这篇关于获取Linux中Java线程的线程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!