ThreadGroup in Java

Java provides a convenient way to group multiple threads in a single object. In such way, we can suspend, resume or interrupt group of threads by a single method call. Java thread group is implemented by java.lang.ThreadGroup class. A ThreadGroup represents a set of threads. A thread group can also include the other thread group. The thread group creates a tree in which every thread group except the initial thread group has a parent. A thread is allowed to access information about its own thread group, but it cannot access the information about its thread group's parent thread group or any other thread groups. There are only two constructors of ThreadGroup class.


Table 6.3: Threadgroup
No. Constructor Description
1 ThreadGroup(String name) creates a thread group with given name.
2 ThreadGroup(ThreadGroup parent, String name) creates a thread
    group with given parent group and name.


There are many methods in ThreadGroup class. A list of ThreadGroup methods are given below.


Table 6.4: Methods for ThreadGroup class
S.N. Modifier and Type Method Description
1 void checkAccess() This method determines if the
    currently running thread has permission to modify the
    thread group.
2 int activeCount() This method returns an estimate of
    the number of active threads in the thread group and
    its subgroups.
3 int activeGroupCount() This method returns an
    estimate of the number of active groups in the
    thread group and its subgroups.
4 void destroy() This method destroys the thread group
    and all of its subgroups.
5 int enumerate(Thread[] list) This method copies into
    the specified
    array every active thread in the thread group and
    its subgroups.
6 int getMaxPriority() This method returns the
    maximum priority of the thread group.
7 String getName() This method returns the name of
    the thread group.
8 ThreadGroup getParent() This method returns the parent of
    the thread group.
9 void interrupt() This method interrupts all threads
    in the thread group.
10 boolean isDaemon() This method tests if the thread
11 void setDaemon(boolean daemon) This method changes
    the daemon status of the thread group.
12 boolean isDestroyed() This method tests if this
    thread group has been destroyed.
13 void list() This method prints information
    about the thread group to the standard output.
14 boolean parentOf(ThreadGroup g This method
    tests if the thread group is either the thread group
    argument or one of its ancestor thread groups.
15 void suspend() This method is used to suspend all
    threads in the thread group.
16 void resume() This method is used to resume all
    threads in the thread group which was suspended using
    suspend() method.
17 void setMaxPriority(int pri) This method sets
    the maximum priority of the group.
18 void stop() This method is used to stop all
    threads in the thread group.
19 String toString() This method returns a string
    representation of the Thread group.


Let's see a code to group multiple threads.


\begin{lstlisting}
ThreadGroup tg1 = new ThreadGroup(''Group A'');
Thread t1 = ...
...o'');
Thread t3 = new Thread(tg1,new MyRunnable(),''three'');
\end{lstlisting}

Now all 3 threads belong to one group. Here, tg1 is the thread group name, MyRunnable is the class that implements Runnable interface and "one", "two" and "three" are the thread names. Now we can interrupt all threads by a single line of code only.


\begin{lstlisting}
Thread.currentThread().getThreadGroup().interrupt();
\end{lstlisting}

ThreadGroup Example

File: ThreadGroupDemo.java


\begin{lstlisting}
public class ThreadGroupDemo implements Runnable{
public vo...
...read Group Name: ''+tg1.getName());
tg1.list();
\par
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
one
two
three
Thread Group Name: Parent ThreadGroup
java.lang...
...d[two,5,Parent ThreadGroup]
Thread[three,5,Parent ThreadGroup]
\end{lstlisting}