Java - ThreadGroup activeCount() 方法



描述

Java ThreadGroup activeCount() 方法返回此執行緒組中活動執行緒數量的估計值。

宣告

以下是 java.lang.ThreadGroup.activeCount() 方法的宣告

public int activeCount()

引數

返回值

此方法返回此執行緒組以及任何其他以該執行緒組為祖先的執行緒組中活動執行緒數量的估計值。

異常

獲取 ThreadGroup 中執行緒的活動數量示例

以下示例展示了在單個 ThreadGroup 物件的情況下使用 ThreadGroup activeCount() 方法的情況。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。然後,我們使用前面建立的 ThreadGroup 物件建立了兩個執行緒。使用 activeCount() 方法,我們列印活動執行緒數。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a ThreadGroup
         ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");

         // create a thread
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // display the number of active threads 
         System.out.println("Active threads in \"" + threadGroup.getName() 
            + "\" = " + threadGroup.activeCount());

         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread-0...
Starting Thread-1...
Active threads in "ThreadGroup" = 2
Thread-0 finished executing.
Thread-1 finished executing.

獲取多個 ThreadGroup 物件中執行緒的活動數量示例

以下示例展示了在多個 ThreadGroup 物件的情況下使用 ThreadGroup activeCount() 方法的情況。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的 ThreadGroup 物件建立了兩個執行緒。使用 activeCount() 方法,我們列印父 ThreadGroup 物件和子 ThreadGroup 物件中的活動執行緒數。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");

         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // display the number of active threads 
         System.out.println("Active threads in \"" + pThreadGroup.getName() 
            + "\" = " + pThreadGroup.activeCount());
			
         System.out.println("Active threads in \"" + cThreadGroup.getName() 
            + "\" = " + cThreadGroup.activeCount());

         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread-0...
Starting Thread-1...
Active threads in "Parent ThreadGroup" = 2
Active threads in "Child ThreadGroup" = 1
Thread-0 finished executing.
Thread-1 finished executing.

獲取子 ThreadGroup 中執行緒的活動數量示例

以下示例展示了在子 ThreadGroup 物件的情況下使用 ThreadGroup activeCount() 方法的情況。我們建立了一個 ThreadGroup 物件併為其分配了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的子 ThreadGroup 物件建立了兩個執行緒。使用 activeCount() 方法,我們列印父 ThreadGroup 物件和子 ThreadGroup 物件中的活動執行緒數。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");

         // create a thread
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // display the number of active threads 
         System.out.println("Active threads in \"" + pThreadGroup.getName() 
            + "\" = " + pThreadGroup.activeCount());
			
         System.out.println("Active threads in \"" + cThreadGroup.getName() 
            + "\" = " + cThreadGroup.activeCount());

         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread-0...
Starting Thread-1...
Active threads in "Parent ThreadGroup" = 2
Active threads in "Child ThreadGroup" = 2
Thread-1 finished executing.
Thread-0 finished executing.
java_lang_threadgroup.htm
廣告