Java.lang.Thread.enumerate() 方法
描述
java.lang.Thread.enumerate() 方法将当前线程的线程组及其子组中的每个活动线程复制到指定的数组中。 它使用 tarray 参数调用当前线程的线程组的枚举方法。
声明
以下是 java.lang.Thread.enumerate() 方法的声明。
public static int enumerate(Thread[] tarray)
参数
tarray − 这是一个要复制到的 Thread 对象数组。
返回值
该方法返回放入数组的线程数
异常
SecurityException − 如果安全管理器存在且其 checkAccess 方法不允许该操作。
示例
下面的例子展示了 java.lang.Thread.enumerate() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ThreadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("Admin Thread");
// prints the current thread
System.out.println("Thread = " + t);
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 = Thread[Admin Thread,5,main] currently active threads = 1 0: Thread[Admin Thread,5,main]

