enumerate_Java Thread类的static int enumerate(Thread [] th)方法与示例

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]


翻译自: https://www.includehelp.com/java/thread-class-static-int-enumerate-thread-th-method-with-example.aspx

enumerate

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值