java get thread count

Description

The java.lang.Thread.activeCount() method returns the number of active threads in the current thread’s thread group.

Declaration

Following is the declaration for java.lang.Thread.activeCount() method

public static int activeCount()

 

顯示所有的 Threads 資訊:

      int count = Thread.activeCount();
      System.out.println("currently active threads = " + count);
    
      Thread th[] = new Thread[count];
      // returns the number of threads put into the array 
      Thread.enumerate(th);
    
      // prints active threads
      for (int i = 0; i < count; i++) {
         System.out.println(i + ": " + th[i]);
      }

開 Thread 時,最好assign thread 名字,這樣子才能透過這麼方法看到 thread 名字。給 thread 名字範例:

 syncDBtask = new SyncAccessControlDB(config.mainDB);
 syncDBtask.setName("syncDBtask Thread");
 syncDBtask.start();

 

 

 

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *