Java Thread.enumerate() Method
Thread.enumerate() method in Java is used to retrieve all active threads in the current thread's thread group, and it stores these threads in an array. This method provides a way to inspect the currently running threads in a program. It is a static method in the Thread class, which means it can be called on the Thread class itself rather than on a specific thread object.
Example 1: Below is an example that demonstrates the usage of Thread.enumerate().
// Java program to demonstrate the
// use of Thread.enumerate() method
class thread extends Thread
{
public void run()
{
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Geeks
{
public static void main(String[] args)
{
thread t1 = new thread();
thread t2 = new thread();
thread t3 = new thread();
thread t4 = new thread();
t1.start();
t2.start();
t3.start();
// Providing a small array that
// cannot hold all threads
Thread[] Active_Threads = new Thread[3];
// use to find the active threads
int activeThreadCount
= Thread.enumerate(Active_Threads);
System.out.println(
"Active threads (limited by array size):");
for (int i = 0; i < activeThreadCount; i++) {
System.out.println(Active_Threads[i].getName());
}
}
}
// Java program to demonstrate the
// use of Thread.enumerate() method
class thread extends Thread
{
public void run()
{
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Geeks
{
public static void main(String[] args)
{
thread t1 = new thread();
thread t2 = new thread();
thread t3 = new thread();
thread t4 = new thread();
t1.start();
t2.start();
t3.start();
// Providing a small array that
// cannot hold all threads
Thread[] Active_Threads = new Thread[3];
// use to find the active threads
int activeThreadCount
= Thread.enumerate(Active_Threads);
System.out.println(
"Active threads (limited by array size):");
for (int i = 0; i < activeThreadCount; i++) {
System.out.println(Active_Threads[i].getName());
}
}
}
Output
Active threads (limited by array size): main Thread-0 Thread-1
Explanation: In the above example, we create some thread objects and start them, which tries to enumerate all active threads using Thread.enumerate(), storing them in an array. Then it prints the names of the captured threads and highlights the limitations of Thread.enumerate() and the potential for missing threads if the provided array is too small.
Syntax of Thread.enumerate() Method
public static int enumerate(Thread[] threads) // Array of threads
- Parameter: It takes a single parameter, threads, which is an array of Thread objects, and populates this array with the active threads.
- Return type: It returns an integer, the number of threads that have been placed into the threads array.
Example 2: In this example, we perform specific tasks on each active thread, such as logging the thread's name or performing cleanup operations.
// Java program to perform specific
// tasks on active threads
class Geeks extends Thread
{
// used to store values
private String v;
public Geeks(String v) {
this.v = v;
}
public void run()
{
try {
System.out.println(v + " is starting.");
Thread.sleep(100);
System.out.println(v + " has finished.");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
Geeks g1 = new Geeks("Thread 1");
Geeks g2 = new Geeks("Thread 2");
// Start threads
g1.start();
g2.start();
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Enumerate active threads
// and log their names
Thread[] activeThreads
= new Thread[Thread.activeCount()];
int activeThreadCount
= Thread.enumerate(activeThreads);
for (int i = 0; i < activeThreadCount-1; i++) {
System.out.println(
"Logging thread: "
+ activeThreads[i].getName());
}
for (int i = 0; i < activeThreadCount-1; i++) {
if (activeThreads[i].isAlive()) {
System.out.println(
activeThreads[i].getName()
+ " is still running.");
}
else {
System.out.println(
activeThreads[i].getName()
+ " has finished.");
}
}
}
}
// Java program to perform specific
// tasks on active threads
class Geeks extends Thread
{
// used to store values
private String v;
public Geeks(String v) {
this.v = v;
}
public void run()
{
try {
System.out.println(v + " is starting.");
Thread.sleep(100);
System.out.println(v + " has finished.");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
Geeks g1 = new Geeks("Thread 1");
Geeks g2 = new Geeks("Thread 2");
// Start threads
g1.start();
g2.start();
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Enumerate active threads
// and log their names
Thread[] activeThreads
= new Thread[Thread.activeCount()];
int activeThreadCount
= Thread.enumerate(activeThreads);
for (int i = 0; i < activeThreadCount-1; i++) {
System.out.println(
"Logging thread: "
+ activeThreads[i].getName());
}
for (int i = 0; i < activeThreadCount-1; i++) {
if (activeThreads[i].isAlive()) {
System.out.println(
activeThreads[i].getName()
+ " is still running.");
}
else {
System.out.println(
activeThreads[i].getName()
+ " has finished.");
}
}
}
}
Output
Thread 2 is starting. Thread 1 is starting. Thread 2 has finished. Thread 1 has finished.
Explanation: In the above example, we created two Geeks threads, started them, and then used Thread.enumerate() to get an array of currently active threads. It then iterates through the array, logging the name of each thread and checking if it is still alive. This example explains how to obtain information and perform actions on active threads.
Important points:
- It is used for checking currently active threads in the JVM.
- It prints the array of thread objects.
- It helps in the inspection and analysis of running threads.