如何获取Java进程中的线程数


Answers:


54

调试Java程序的有用工具,它提供了线程数和其他有关线程的信息:

jconsole <process-id>



79

ManagementFactory.getThreadMXBean().getThreadCount()不会像线程组那样限制自己Thread.activeCount()


+1为此。线程的数量与我看到的完全相同top。虽然在调试窗口中,但我只看到2个线程正在运行,而不是5个。:/
Aruman 2013年

管理工厂?在哪个包装下?
Meher 2014年

import java.lang.management.ManagementFactory; docs.oracle.com/javase/8/docs/api/java/lang/management/…getPeakThreadCount()自己开始做之前, 请参见和其他方法:)
Christophe Roussy

24

类上有一个静态方法,该方法Thread将返回由JVM控制的活动线程数:

Thread.activeCount()

返回当前线程的线程组中活动线程的数量。

此外,如果您希望实时监视它们,则外部调试器应列出所有活动线程(并允许您暂停任何数量的活动线程)。



9

我编写了一个程序来迭代所有Threads创建和打印getState()的程序Thread

import java.util.Set;

public class ThreadStatus {
    public static void main(String args[]) throws Exception{
        for ( int i=0; i< 5; i++){
            Thread t = new Thread(new MyThread());
            t.setName("MyThread:"+i);
            t.start();
        }
        int threadCount = 0;
        Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
        for ( Thread t : threadSet){
            if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup()){
                System.out.println("Thread :"+t+":"+"state:"+t.getState());
                ++threadCount;
            }
        }
        System.out.println("Thread count started by Main thread:"+threadCount);
    }
}

class MyThread implements Runnable{
    public void run(){
        try{
            Thread.sleep(2000);
        }catch(Exception err){
            err.printStackTrace();
        }
    }
}

输出:

java ThreadStatus

Thread :Thread[MyThread:0,5,main]:state:TIMED_WAITING
Thread :Thread[main,5,main]:state:RUNNABLE
Thread :Thread[MyThread:1,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:4,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:2,5,main]:state:TIMED_WAITING
Thread :Thread[MyThread:3,5,main]:state:TIMED_WAITING
Thread count started by Main thread:6

如果您删除以下情况

if ( t.getThreadGroup() == Thread.currentThread().getThreadGroup())

您还将在输出中得到以下线程,这些线程已由系统启动。

Reference Handler, Signal Dispatcher,Attach Listener and Finalizer


5

使用LinuxTop命令

top -H -p (process id)

您可以通过以下方法获得一个程序的进程ID:

ps aux | grep (your program name)

例如 :

ps aux | grep user.py


1
    public class MainClass {

        public static void main(String args[]) {

          Thread t = Thread.currentThread();
          t.setName("My Thread");

          t.setPriority(1);

          System.out.println("current thread: " + t);

          int active = Thread.activeCount();
          System.out.println("currently active threads: " + active);
          Thread all[] = new Thread[active];
          Thread.enumerate(all);

          for (int i = 0; i < active; i++) {
             System.out.println(i + ": " + all[i]);
          }
       }
   }

0

使用jstack获取线程数

jstack <PID> | grep 'java.lang.Thread.State' | wc -l

上面代码的结果与top -H -p <PID>或截然不同ps -o nlwp <PID>jstack是因为jstack仅从应用程序创建的线程中获取线程。

换句话说,jstack不会获取GC线程

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.