java.lang.Thread.enumerate()%uA0方法复制到指定的数组当前线程的线程组及其子组中的每一个活动线程。它调用当前线程的线程组,为tarray参数的枚举方法。
声明
以下是java.lang.Thread.enumerate()方法的声明
public static int enumerate(Thread[] tarray)
参数
-
tarray%uA0-- 此是线程对象复制到一个数组。
返回值
此方法返回放入数组的线程数
异常
-
SecurityException%uA0-- 如果安全管理器存在并且其checkAccess方法不允许该操作。
例子
下面的例子显示java.lang.Thread.enumerate()方法的使用。
package com.yiibai 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]