How to get the number of threads in a Java process

后端 未结 9 759
无人及你
无人及你 2020-12-07 19:49

How can I see the number of threads in a Java process?

相关标签:
9条回答
  • 2020-12-07 20:14

    Useful tool for debugging java programs, it gives the number of threads and other relevant info on them:

    jconsole <process-id>

    0 讨论(0)
  • 2020-12-07 20:22

    I have written a program to iterate all Threads created and printing getState() of each 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();
            }
        }
    }
    

    Output:

    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 you remove below condition

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

    You will get below threads in output too, which have been started by system.

    Reference Handler, Signal Dispatcher,Attach Listener and Finalizer.

    0 讨论(0)
  • 2020-12-07 20:22

    Using Linux Top command

    top -H -p (process id)

    you could get process id of one program by this method :

    ps aux | grep (your program name)

    for example :

    ps aux | grep user.py

    0 讨论(0)
  • 2020-12-07 20:23

    ManagementFactory.getThreadMXBean().getThreadCount() doesn't limit itself to thread groups as Thread.activeCount() does.

    0 讨论(0)
  • 2020-12-07 20:25
    java.lang.Thread.activeCount()
    

    It will return the number of active threads in the current thread's thread group.

    docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#activeCount()

    0 讨论(0)
  • 2020-12-07 20:34

    Get number of threads using jstack

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

    The result of the above code is quite different from top -H -p <PID> or ps -o nlwp <PID> because jstack gets only threads from created by the application.

    In other words, jstack will not get GC threads

    0 讨论(0)
提交回复
热议问题