Java Thread enumerate()方法

java.lang.Thread.enumerate() 方法复制到指定的数组当前线程的线程组及其子组中的每一个活动线程。它调用当前线程的线程组,为tarray参数的枚举方法。

1 语法

public static int enumerate(Thread[] tarray)

2 参数

tarray:这是线程对象复制到一个数组。

3 返回值

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

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.lang.Thread.enumerate()方法的例子
 */
import java.lang.*;

public class Demo {

    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 = 2
0: Thread[Admin Thread,5,main]
1: Thread[Monitor Ctrl-Break,5,main]

热门文章

优秀文章