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];
currentGroup.enumerate(listOfThreads);
for (int i = 0; i < numThreads; i++)
System.out.println("Thread #" + i + " = " +
listOfThreads[i].getName());
}
}
Other collection management methods provided by the ThreadGroup
class include activeGroupCount
and list
.
No comments:
Post a Comment