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.
8 Answers
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

- 6,970
- 10
- 42
- 65
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 java
process 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 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 might return the LWP number that you see in "ps -eLf" output, but I'm not sure.

- 33,849
- 3
- 67
- 87
-
2you can correlate that with the thread id from `jstack`. You'll need to convert from hex to decimal. – ninjalj Mar 30 '12 at 00:28
-
@ninjalj: That could have been a nice answer. :) – sarnold Mar 30 '12 at 22:29
-
2The 'nid' value from jstack will match 'tid' from Linux ps output. You will have to convert the hex value to decimal. – rohitmohta Jan 23 '15 at 18:07
All of the methods mentioned here work just fine. I was also searching for something similar and came across this blog by Timur Akhmadeev. I hope it helps.
Edit:
As I was pointed by fellow programmers, the following is a summary of the post by Timur:
On a *nux based system first do a
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
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.
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.

- 71
- 1
- 3
The ps(1)
thread-selector switch H
will ask ps(1)
to show threads as processes. (Which is roughly how they're implemented anyway.)
See:
$ ps axl | wc -l
163
$ ps axlH | wc -l
325
Apparently I've got a lot of threaded processes running right now.

- 102,305
- 22
- 181
- 238
-
1
-
To get a list of running java processes and the number of respective threads: ps -Haxo pid,stat,cmd | grep -v grep | grep java | sort | uniq -c | sort -nr – FuePi Feb 28 '20 at 15:08
- find threads ID by top with option H "on", and write down threads PIDs
- make thread dump and find the stack of your thread. There are PIDs in hex format.

- 56
- 2
Since java 1.8 you can use jcmd
docs.oracle.com
list of runing jvm
jcmd -l
stack of threads inside jvm
jcmd 123 Thread.print

- 53
- 5
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.
Example: native code
JNIEXPORT jlong JNICALL Java_nativeGetThreadId(JNIEnv * env, jobject obj) {
jlong threadId;
threadId = (jlong)gettid();
return threadId;
}

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

- 18,003
- 15
- 89
- 143
-
Visual VM is probably the best tool given that you have ports open to target server. – Petro Semeniuk Mar 30 '12 at 02:25