Java - ThreadGroup enumerate(Thread[] list) 方法



描述

Java ThreadGroup enumerate(Thread[] list) 方法将此线程组及其子组中的每个活动线程复制到指定的数组中。

声明

以下是 java.lang.ThreadGroup.enumerate(Thread[] list) 方法的声明

public int enumerate(Thread[] list)

参数

list − 这是要将线程列表放入的数组。

返回值

此方法返回放入数组中的线程数。

异常

SecurityException − 如果当前线程没有权限枚举此线程组。

枚举 ThreadObject 对象列表示例

以下示例展示了在单个 ThreadGroup 对象的情况下使用 ThreadGroup enumerate(Thread[] list) 方法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。然后,我们使用前面创建的线程组对象创建了两个线程。使用 enumerate() 方法,我们将所有活动线程放入数组中,然后使用 for 循环在数组上打印它们的名称。

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();

         // returns the number of threads put into the array
         Thread[] list = new Thread[threadGroup.activeCount()];
         int count = threadGroup.enumerate(list);
         for (int i = 0; i < count; i++) {
            System.out.println("Thread " + list[i].getName() + " found.");
         }

         // 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...
Thread Thread-0 found.
Thread Thread-1 found.
Thread-0 finished executing.
Thread-1 finished executing.

在多个 ThreadGroup 对象中枚举 ThreadObject 对象列表示例

以下示例展示了在多个 ThreadGroup 对象的情况下使用 ThreadGroup enumerate(Thread[] list) 方法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的线程组对象创建了两个线程。使用 enumerate() 方法,我们将子 ThreadGroup 对象中的所有活动线程放入数组中,然后使用 for 循环在数组上打印它们的名称。由于子 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();

         // returns the number of threads put into the array
         Thread[] list = new Thread[cThreadGroup.activeCount()];
         int count = cThreadGroup.enumerate(list);
         for (int i = 0; i < count; i++) {
            System.out.println("Thread " + list[i].getName() + " found.");
         }
            
         // 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...
Thread Thread-1 found.
Thread-0 finished executing.
Thread-1 finished executing.

在子/孙 ThreadGroup 对象中枚举 ThreadObject 对象列表示例

以下示例展示了在子线程组和孙线程组对象的情况下使用 ThreadGroup enumerate(Thread[] list) 方法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用前面创建的子线程组和孙线程组对象创建了两个线程。使用 enumerate() 方法,我们将从祖父 ThreadGroup 对象中获取所有活动线程放入数组中,然后使用 for 循环在数组上打印它们的名称。

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();
            
         // returns the number of threads put into the array
         Thread[] list = new Thread[pThreadGroup.activeCount()];
         int count = pThreadGroup.enumerate(list);
         for (int i = 0; i < count; i++) {
            System.out.println("Thread " + list[i].getName() + " found.");
         }

         // 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...
Thread Thread-0 found.
Thread Thread-1 found.
Thread-1 finished executing.
Thread-0 finished executing.
java_lang_threadgroup.htm
广告