ThreadGroup - 1

"Hi, Amigo!"

"We're going to start a more thorough exploration of threads."

"The concept of a thread group was introduced to prevent a thread from repeatedly stopping and interrupting every other thread. A thread can only affect other threads in the same thread group. ThreadGroup is a class for managing thread groups. This approach lets you protect threads from unwanted changes."

"Sometimes you have to run code that you can't fully trust. So it's convenient to put all of its threads in a separate group and block them from interfering with the work of the main thread group."

"A thread group can contain other groups. This lets you organize all your threads and groups in a hierarchical tree. In such a tree, each thread group (except the initial group) has its own parent."

"The ThreadGroup class has methods that let you get lists of all threads and affect/change them. When we create a new thread without explicitly specifying a group, it joins the same group as the creator thread."

"Here are some of the methods in the ThreadGroup class:"

Method Description
String getName()
Returns the group name
ThreadGroup getParent()
Returns the parent group
void interrupt()
Interrupts all threads in the group.
boolean isDaemon()
Checks whether the group is a daemon
void setDaemon(boolean daemon)
Sets the group's daemon property
int activeCount()
Returns the number of live threads in the group and its subgroups
int activeGroupCount()
Returns the number of live groups in the group and its subgroups
int enumerate(Thread[] list)
Puts all live threads into the array and returns the number of them.
int getMaxPriority()
Returns the maximum priority for threads in the group.
void setMaxPriority(int priority)
Lets you set the maximum priority of threads in the group and subgroups.