通过/proc伪文件系统。cat /proc/{pid}/status
其中Threads后面跟的就是线程数。
或者:通过
ls /proc/{pid}/task | wc -ltop命令后面跟-H,会打印出所有线程列表top -H top -H -p {pid}ps 后面加上H,能打印某个进程的所有线程ps hH p {pid} | wc -l
显示结果:
PID TTY STAT TIME COMMAND 17721 pts/1 Sl 0:00 /usr/java/jdk1.8.0_60/bin/java -Djava.util.logging.config.file=/mnt/deploy/apache-tomcat-8.0.35/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoader 17721 pts/1 Sl 0:00 /usr/java/jdk1.8.0_60/bin/java -Djava.util.logging.config.file=/mnt/deploy/apache-tomcat-8.0.35/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoader 17721 pts/1 Sl 0:00 /usr/java/jdk1.8.0_60/bin/java -Djava.util.logging.config.file=/mnt/deploy/apache-tomcat-8.0.35/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoader
使用ps