Java - ThreadGroup activeGroupCount() 方法



描述

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

宣告

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

public int activeGroupCount()

引數

返回值

此方法返回以此執行緒組為祖先的活動執行緒組的數量。

異常

在 ThreadGroup 物件中獲取活動組計數示例

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

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 groups in \"" + threadGroup.getName() 
            + "\" = " + threadGroup.activeGroupCount());

         // 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 groups in "ThreadGroup" = 0
Thread-1 finished executing.
Thread-0 finished executing.

在多個 ThreadGroup 物件中獲取活動組計數示例

以下示例展示了在多個 ThreadGroup 物件的情況下 ThreadGroup activeGroupCount() 方法的用法。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的 threadgroup 物件建立了兩個執行緒。使用 activeGroupCount() 方法,我們列印父 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 groups in \"" + pThreadGroup.getName() 
            + "\" = " + pThreadGroup.activeGroupCount());
			
         System.out.println("Active groups in \"" + cThreadGroup.getName() 
            + "\" = " + cThreadGroup.activeGroupCount());

         // 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 groups in "Parent ThreadGroup" = 1
Active groups in "Child ThreadGroup" = 0
Thread-1 finished executing.
Thread-0 finished executing.

在子/孫 ThreadGroup 物件中獲取活動組計數示例

以下示例展示了在子 ThreadGroup 物件和孫 ThreadGroup 物件的情況下 ThreadGroup activeGroupCount() 方法的用法。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的子 ThreadGroup 物件和孫 ThreadGroup 物件建立了兩個執行緒。使用 activeGroupCount() 方法,我們列印所有 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 grandchild ThreadGroup for parent ThreadGroup
         ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild 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(gThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // display the number of active threads 
         System.out.println("Active groups in \"" + pThreadGroup.getName() 
            + "\" = " + pThreadGroup.activeGroupCount());
			
         System.out.println("Active groups in \"" + cThreadGroup.getName() 
            + "\" = " + cThreadGroup.activeGroupCount());
			
         System.out.println("Active groups in \"" + gThreadGroup.getName() 
            + "\" = " + gThreadGroup.activeGroupCount());

         // 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 groups in "Parent ThreadGroup" = 2
Active groups in "Child ThreadGroup" = 1
Active groups in "GrandChild ThreadGroup" = 0
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
廣告