I would like to monitor the number of threads used by a specific process on Linux. Is there an easy way to get this information without impacting the performance of the proc
Newer JDK distributions ship with JConsole and VisualVM. Both are fantastic tools for getting the dirty details from a running Java process. If you have to do this programmatically, investigate JMX.
VisualVM can show clear states of threads of a given JVM process
If you want the number of threads per user in a linux system then you should use:
ps -eLf | grep <USER> | awk '{ num += $6 } END { print num }'
where as <USER>
use the desired user name.
My answer is more gui, but still within terminal. Htop may be used with a bit of setup.
If you use:
ps uH p <PID_OF_U_PROCESS> | wc -l
You have to subtract 1 to the result, as one of the lines "wc" is counting is the headers of the "ps" command.
Each thread in a process creates a directory under /proc/<pid>/task
. Count the number of directories, and you have the number of threads.