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 process?
-
How about if an application's process only runs for a short time? (Say 2 seconds.) – user2023370 Nov 22 '19 at 17:42
19 Answers
-
3I believe you should subtract `1` from it because it prints a line like `USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND` for table header. – ahmet alp balkan Apr 27 '12 at 23:40
-
3
-
26-1 Why pipe the output to `wc` when you could just `ps -o thcount
`? See [this answer](https://stackoverflow.com/a/37713259/194894). – Flow Nov 04 '17 at 20:03 -
1
-
4andrzejdoro, you can enable it in htop setting, just press F2(setup) - Setup Columns - select NLWP in Available Columns and move it into Activated Columns - F10 for save – slav0nic Sep 17 '20 at 10:17
To get the number of threads for a given pid:
$ ps -o nlwp <pid>
Where nlwp
stands for Number of Light Weight Processes (threads). Thus ps
aliases nlwp
to thcount
, which means that
$ ps -o thcount <pid>
does also work.
If you want to monitor the thread count, simply use watch
:
$ watch ps -o thcount <pid>
To get the sum of all threads running in the system:
$ ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'

- 1,707
- 1
- 10
- 8
-
3Most useful answer here. Especially the `watch` command. However, note that using `thcount` can fail for some (Red Hat...), though `nlwp` worked for me. – Jan 17 '18 at 06:20
-
2To get the process id of a given process name use `pidof` or `pgrep`. E.g., `ps -o nlwp $(pidof chrome)` or `ps -o nlwp $(pgrep chrome)`. – hmofrad Dec 08 '20 at 18:20
Each thread in a process creates a directory under /proc/<pid>/task
. Count the number of directories, and you have the number of threads.

- 224,562
- 31
- 268
- 324
-
2To add to the above comment. You can use this command to find the number of threads: `find /proc/
/task -maxdepth 1 -type d -print | wc -l`. Just replace the _ – Navigatron Oct 14 '16 at 10:15_ with your process ID that you can get from `top` or using `ps` -
1
cat /proc/<PROCESS_PID>/status | grep Threads

- 347,512
- 102
- 1,199
- 985

- 7,525
- 1
- 36
- 40
ps -eLf
on the shell shall give you a list of all the threads and processes currently running on the system.
Or, you can run top
command then hit 'H' to toggle thread listings.
-
This is the sauce for me. I don't want to limit it only to one process. It's easy to add a `-p` to this if necessary, or anything else. This is the minimum you need to see the thread list. – Erick Robertson Apr 06 '16 at 14:45
$ ps H p pid-id
H - Lists all the individual threads in a process
or
$cat /proc/pid-id/status
pid-id is the Process ID
eg.. (Truncated the below output)
root@abc:~# cat /proc/8443/status
Name: abcdd
State: S (sleeping)
Tgid: 8443
VmSwap: 0 kB
Threads: 4
SigQ: 0/256556
SigPnd: 0000000000000000

- 20,781
- 11
- 57
- 77

- 91
- 1
- 3
My answer is more gui, but still within terminal. Htop may be used with a bit of setup.
- Start htop.
- Enter setup menu by pressing F2.
- From leftmost column choose "Columns"
- From rightmost column choose the column to be added to main monitoring output, "NLWP" is what you are looking for.
- Press F10.

- 293
- 7
- 13
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.

- 141
- 1
- 1
-
Welcome to StackOverflow. Arguably, this should be a comment to the answer by slav0nic. However, as I understand it, when you first join SO, you (still) can't comment on answers until you've gained some reputation, so adding an answer is about all you can do. You are correct; you should not count the header line from `ps` as one of the threads. – Jonathan Leffler Aug 28 '11 at 00:11
Here is one command that displays the number of threads of a given process :
ps -L -o pid= -p <pid> | wc -l
Unlike the other ps
based answers, there is here no need to substract 1
from its output as there is no ps
header line thanks to the -o pid=
option.

- 29,783
- 6
- 61
- 72
JStack is quite inexpensive - one option would be to pipe the output through grep to find active threads and then pipe through wc -l.
More graphically is JConsole, which displays the thread count for a given process.

- 246
- 1
- 3
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.

- 29,624
- 9
- 57
- 79
If you're looking for thread count for multiple processes, the other answers won't work well for you, since you won't see the process names or PIDs, which makes them rather useless. Use this instead:
ps -o pid,nlwp,args -p <pid_1> <pid_2> ... <pid_N>
In order to watch the changes live, just add watch
:
watch ps -o pid,nlwp,args -p <pid_1> <pid_2> ... <pid_N>

- 397
- 1
- 6
- 15
-
1I wanted to show just amount of threads for all process of a specific user. This answer inspired the following: watch ps -u
-o pid,nlwp,args – Puterdo Borato Jul 29 '21 at 12:41
The easiest way is using "htop". You can install "htop" (a fancier version of top) which will show you all your cores, process and memory usage.
Press "Shift+H" to show all process or press again to hide it. Press "F4" key to search your process name.
Installing on Ubuntu or Debian:
sudo apt-get install htop
Installing on Redhat or CentOS:
yum install htop
dnf install htop [On Fedora 22+ releases]
If you want to compile "htop" from source code, you will find it here.

- 1,112
- 12
- 11
If you are trying to find out the number of threads using cpu for a given pid I would use:
top -bc -H -n2 -p <pid> | awk '{if ($9 != "0.0" && $1 ~ /^[0-9]+$/) print $1 }' | sort -u | wc -l

- 56
- 3
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.

- 422
- 3
- 11
If you're interested in those threads which are really active -- as in doing something (not blocked, not timed_waiting, not reporting "thread running" but really waiting for a stream to give data) as opposed to sitting around idle but live -- then you might be interested in jstack-active.
This simple bash script runs jstack
then filters out all the threads which by heuristics seem to be idling, showing you stack traces for those threads which are actually consuming CPU cycles.

- 6,508
- 3
- 27
- 16
First get the process ID (pid) by executing below command:
ps -ef | grep (for e.g ps -ef | grep java)
Now replace the pid in below command and execute to get the total thread count of a process.
ps huH p | wc -l

- 21
- 3