Home »
Java programming language
Java ThreadGroup enumerate() method with example
ThreadGroup Class enumerate() method: Here, we are going to learn about the enumerate() method of ThreadGroup Class with its syntax and example.
Submitted by Preeti Jain, on December 23, 2019
ThreadGroup Class enumerate() method
Syntax:
public int enumerate (Thread[] th1);
public int enumerate (Thread[] th2, boolean recurr1);
public int enumerate (ThreadGroup[] tg1);
public int enumerate (ThreadGroup[] tg2, boolean recurr2);
- enumerate() method is available in java.lang package.
- enumerate (Thread[] th1) method is used to copies all active threads in a thread group and placed into the given array Thread[].
- enumerate (Thread[] th2, boolean recurr1) method is used to copies all active thread in a thread group and placed into the given array Thread[] but if the given boolean variable is set to true then reference to every active thread in this thread subgroup are also included.
- enumerate (ThreadGroup[] tg1) method is used to copies all active subgroups in a thread group and placed into the given array ThreadGroup[].
- enumerate (ThreadGroup[] tg2, boolean recurr2) method is used to copies all active subgroups in a thread group and placed into the given array ThreadGroup[] but if the given boolean variable is set to true then reference to every active subgroup in this subgroups are also included. These methods may throw an exception at the time of copies of all active threads in this thread group.
SecurityException – This exception may throw when the current thread is not permitted to enumerate this ThreadGroup.
- These methods are non-static methods, it is accessible with class objects only and, if we try to access these methods with the class name then we will get an error.
Parameter(s):
- In the first case, Thread[] th1 – represents an array of "Thread" types into which to place the set of copied threads.
-
In the second case, Thread[] th1, boolean recurr1
- Thread[] th1 – Similar as defined in the first case.
- boolean recurr1 – represents a flag status denoting to include threads is thread group that is subgroups of this thread group.
- In the third case, ThreadGroup[] tg1 – represents an array of "ThreadGroup" type into which to place the set of the copied thread group.
-
In the fourth case, ThreadGroup[] tg2, boolean recurr2
- ThreadGroup[] tg2 – Similar as defined in the third case.
- boolean recurr2 – represents a flag status denoting to include thread groups.
Return value:
In the first case, the return type of the method is int – It returns the count of the number of threads placed into an array.
In the second case, the return type of the method is int – Similar as defined in the first case.
In the third case, the return type of the method is int – It returns the count of the number of thread groups placed in an array.
In the fourth case, the return type of the method is int – Similar as defined in the third case.
Example:
// Java program to demonstrate the example
// of enumerate() method of ThreadGroup()class
public class Enumerate implements Runnable {
public static void main(String[] args) {
Enumerate en = new Enumerate();
en.enumerates();
}
public void enumerates() {
try {
// Create two thread group and the named are base
// and derived
ThreadGroup base = new ThreadGroup("Base ThreadGroup");
ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");
// create two threads
Thread th1 = new Thread(base, this);
Thread th2 = new Thread(derived, this);
// By using getName() method is to retrieve the
// name of the thread th1
System.out.println(th1.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th1
th1.start();
// By using getName() method is to retrieve the
// name of the thread th2
System.out.println(th2.getName() + " " + "begins.....");
// By using start() method is to start its execution
// of thread th2
th2.start();
Thread[] t1 = new Thread[base.activeCount()];
ThreadGroup[] tg1 = new ThreadGroup[base.activeGroupCount()];
// By using enumerate() method is to put the
// copied threads in an array
System.out.println();
int cnt1 = base.enumerate(t1);
for (int i = 0; i < cnt1; ++i) {
System.out.print("enumerate(Thread[] t1) :");
System.out.println(t1[i].getName() + " " + "exists");
}
System.out.println();
int cnt2 = base.enumerate(t1, true);
for (int j = 0; j < cnt2; ++j) {
System.out.print("enumerate(Thread[] t1, boolean recurr) :");
System.out.println(t1[j].getName() + " " + "exists");
}
System.out.println();
int cnt3 = base.enumerate(tg1);
for (int k = 0; k < cnt3; ++k) {
System.out.print("enumerate(ThreadGroup[] tg1) :");
System.out.println(tg1[k].getName() + " " + "exists");
}
System.out.println();
int cnt4 = base.enumerate(tg1, true);
for (int l = 0; l < cnt4; ++l) {
System.out.print("enumerate(ThreadGroup[] tg1, boolean recurr) :");
System.out.println(tg1[l].getName() + " " + "exists");
}
// By using join() method is to wait the current
// thread till complete execution of another
// thread
th1.join();
th2.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// Override run()
public void run() {
for (int k = 0; k < 100; ++k)
++k;
System.out.println(Thread.currentThread().getName() + " " + "ends.....");
}
}
Output
Thread-0 begins.....
Thread-1 begins.....
Thread-0 ends.....
Thread-1 ends.....
enumerate(Thread[] t1) :Thread-1 exists
enumerate(ThreadGroup[] tg1) :Derived ThreadGroup exists
enumerate(ThreadGroup[] tg1, boolean recurr) :Derived ThreadGroup exists