static int enumerate(Thread[] tarray)
描述 (Description)
java.lang.Thread.enumerate()方法将当前线程的线程组及其子组中的每个活动线程复制到指定的数组中。 它使用tarray参数调用当前线程的线程组的enumerate方法。
声明 (Declaration)
以下是java.lang.Thread.enumerate()方法的声明
public static int enumerate(Thread[] tarray)
参数 (Parameters)
tarray - 这是要复制到的Thread对象数组。
返回值 (Return Value)
此方法返回放入数组的线程数
异常 (Exception)
SecurityException - 如果存在安全管理器且其checkAccess方法不允许该操作。
例子 (Example)
以下示例显示了java.lang.Thread.enumerate()方法的用法。
package com.iowiki;
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]