enumerate
线程类static int枚举(Thread [] th) (Thread Class static int enumerate(Thread[] th))
This method is available in package java.lang.Thread.enumerate(Thread[] th).
软件包java.lang.Thread.enumerate(Thread [] th)中提供了此方法。
This method is used to copy all the active thread of the current threads thread group or its subgroup into the specified array which will be given as an argument in the method.
此方法用于将当前线程线程组或其子组的所有活动线程复制到指定的数组中,该数组将作为方法中的参数给出。
This method is static so this method is accessible with classname too like Thread.enumerate(Thread[] th).
此方法是静态的,因此也可以使用类名访问此方法,例如Thread.enumerate(Thread [] th) 。
The return type of this method is int it returns the number of active threads which will be kept in the given array as an argument in the method.
此方法的返回类型为int,它返回活动线程的数量,该数量将作为方法的参数保留在给定数组中。
This method does raise an exception if access permission denies to the thread.
如果访问权限拒绝该线程,则此方法会引发异常。
Syntax:
句法:
static int enumerate(Thread[] th){
}
Parameter(s):
参数:
We pass one array of thread type which will keep all active threads of current threads thread group.
我们传递一个线程类型数组,该数组将保留当前线程线程组的所有活动线程。
Return value:
返回值:
The return type of this method is int, it returns the count of all active threads which will be kept in the array as an argument in the method.
此方法的返回类型为int ,它返回所有活动线程的计数,这些计数将作为方法的参数保留在数组中。
Java程序演示enumerate()方法的示例 (Java program to demonstrate example of enumerate() method)
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
public class Enumerate {
public static void main(String[] args) {
// By using currentThread() of Thread class will return
// a reference of currently executing thread.
Thread th = Thread.currentThread();
// By using setName() method we are setting the name of
// current executing thread
th.setName("Enumerate Thread");
// By using setPriority() method we are setting the
// priority of current executing thread
th.setPriority(2);
//Display Current Executing Thread
System.out.println("Currently Executing Thread is :" + th);
int active_thread = Thread.activeCount();
// Display the number of active threads in current threads thread group
System.out.println("The Current active threads is : " + active_thread);
Thread[] thread = new Thread[active_thread];
// active_thread kept in the array
Thread.enumerate(thread);
// Loop for printing active thread if we have more than one thread.
for (int i = 0; i < active_thread; ++i)
System.out.println("Display active threads is " + thread[i]);
}
}
Output
输出量
E:\Programs>javac Enumerate.java
E:\Programs>java Enumerate
Currently Executing Thread is :Thread[Enumerate Thread,2,main]
The Current active threads is : 1
Display active threads is Thread[Enumerate Thread,2,main]
enumerate