获取当前进程中所有线程数

ps -eLf |awk -F ' ' '{x[$2]++;} END{for(i in x) if(x[i] > 100) {print(i ":" x[i])}}'

  1. ps -eLf 得到所有线程明细

字段如下

UID         PID   PPID    LWP  C NLWP STIME TTY          TIME CMD
  1. 通过awk 统计分组功能

-F ' ' 根据空格

{x[$2]++;} 第二个参数 因为这里要获取pid

if(x[i] > 100) 打印大于 100的 过滤出当前线程数多的 进程

结果如下

pid 线程数
396399:368
3206849:407
1730920:360
1727874:340
115744:195

根据 线程多的pid去 定位 问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取当前进程的所有线程,你可以使用操作系统提供的相关函或系统调用。具体的实现方式会根据你使用的编程语言和操作系统而有所不同。以下是一些常见编程语言和操作系统的示例: 在C语言,你可以使用`pthread_self()`函获取当前线程的标识符,然后使用`getpid()`函获取当前进程的标识符。通过遍历所有可能的线程标识符,你可以获取当前进程的所有线程。以下是一个简单的示例: ```c #include <stdio.h> #include <pthread.h> #include <unistd.h> int main() { pid_t pid = getpid(); printf("当前进程ID:%d\n", pid); pthread_t thread; pthread_t self = pthread_self(); printf("当前线程ID:%lu\n", self); // 遍历所有可能的线程ID for (thread = 1; thread <= self; thread++) { if (pthread_equal(thread, self)) { // 打印当前线程ID printf("当前线程ID:%lu\n", thread); } } return 0; } ``` 在Java语言,你可以使用`Thread.currentThread()`方法来获取当前线程对象,然后使用`Thread.getAllStackTraces()`方法获取当前进程的所有线程堆栈信息。以下是一个简单的示例: ```java public class Main { public static void main(String[] args) { long pid = ProcessHandle.current().pid(); System.out.println("当前进程ID:" + pid); Thread thread = Thread.currentThread(); long self = thread.getId(); System.out.println("当前线程ID:" + self); // 获取当前进程的所有线程 Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces(); for (Thread t : threads.keySet()) { if (t.getId() == self) { // 打印当前线程ID System.out.println("当前线程ID:" + t.getId()); } } } } ``` 请注意,以上示例仅为演示如何获取当前进程的所有线程,实际使用时可能需要根据具体情况进行适当的调整和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值