Home » Java programming language

Java Thread Class static int enumerate(Thread[] th) method with Example

Java Thread Class static int enumerate(Thread[] th) method: Here, we are going to learn about the static int enumerate(Thread[] th) method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 18, 2019

Thread Class static int enumerate(Thread[] th)

  • This method is available in package 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).
  • 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.
  • 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.

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]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.