Obtaining the thread ID for Java threads in Linux
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
How to obtain the JVM thread name/id through the known PID/TID
Use jstack <PID>
"Thread-0" prio=10 tid=0x00002aaab01c3800 nid=0x246d runnable [0x00000000423c7000]
^ name ^ Java thread id ^ native (OS) thread id
How to find a Java thread running on Linux with ps -axl?
Use
jps -v
for finding your java process. Sample Output:
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:
.....
"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 -v
sudo jstack 6172
Matching between Java Thread and native Linux thread
thank all of you very much!
It seems that one of the easiest way to get such a behavior is to pass ZERO as threadId and expect thread with such a call being affined to a core you wish.
Of course there is another way to affine a thread to a core ( from Java PL ) but you have to decide upon cpu_set_t type substitution as well as its size.
How to get thread id from a thread pool?
Using Thread.currentThread()
:
private class MyTask implements Runnable {
public void run() {
long threadId = Thread.currentThread().getId();
logger.debug("Thread # " + threadId + " is doing this task");
}
}
The difference between Thread.currentThread().getId() and Process.myTid() in Android
I believe that there are two types of threads in a Java environment:
- Native Thread: which is managed by the hosted OS.
- Java Thread: which is managed by the JVM and is communicated with the native thread.
Related Topics
Installing Rxtx Bundle in My Raspberry
When Should We Use Intern Method of String on String Literals
Non Resizable Window Border and Positioning
How to Make the Method Return Type Generic
When to Use Stringbuilder in Java
Manifest Merger Failed:Attribute Application@Appcomponentfactory Cant Solve This
What Does "|=" Mean? (Pipe Equal Operator)
How to Change Menu Item Text Dynamically in Android
Problems with Java3D Lib Configuration
Find Directory for "Application Data" on Linux and MACintosh
Linux Launch Java Program on Startup (Ec2 Instance)