How to get the number of threads in a Java process

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

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

9条回答
  •  囚心锁ツ
    2020-12-07 20:40

        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]);
              }
           }
       }
    

提交回复
热议问题