Sunday, June 19, 2011

Collection Management Methods in ThreadGroup

The ThreadGroup provides a set of methods that manage the threads and subgroups within the group and allow other objects to query the ThreadGroup for information about its contents. For example, you can call ThreadGroup's activeCount method to find out the number of active threads currently in the group. The activeCount method is often used with the enumerate method to get an array filled with references to all the active threads in a ThreadGroup. For example, the listCurrentThreads method in the following example fills an array with all of the active threads in the current thread group and prints their names:
public class EnumerateTest {
public void listCurrentThreads() {
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
int numThreads = currentGroup.activeCount();
Thread[] listOfThreads = new Thread[numThreads];

for (int i = 0; i < numThreads; i++)
System.out.println("Thread #" + i + " = " +

Other collection management methods provided by the ThreadGroup class include activeGroupCount and list.

No comments:

Post a Comment