Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 573
JohnPristine Avatar asked Mar 29 '12 22:03

JohnPristine


People also ask

How do I see running threads in Linux?

Using the top command The top command can show a real-time view of individual threads. To enable thread views in the top output, invoke top with "-H" option. This will list all Linux threads. You can also toggle on or off thread view mode while top is running, by pressing 'H' key.

How do I monitor Java threads in Linux?

Identifying the thread On Unix® and Linux® systems, you can use the top command: $ top -n 1 -H -p [pid]replacing [pid] with the process ID of the affected process. On Solaris®, you can use the prstat command: $ prstat -L -p [pid]replacing [pid] with the process ID of the affected process.

How can I display the threads per process using ps command in Linux?

In ps command, "-T" option enables thread views. The following command list all threads created by a process with <pid>. The "SID" column represents thread IDs, and "CMD" column shows thread names.

How do I find the pid of a thread in Linux?

In the GNU C Library implementation running on Linux, the process ID is the thread group ID of all threads in the process. You can get the process ID of a process by calling getpid . The function getppid returns the process ID of the parent of the current process (this is also known as the parent process ID).


1 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 
like image 90
Petro Semeniuk Avatar answered Sep 18 '22 23:09

Petro Semeniuk