ThreadGroup in Java
Java provides a convenient way to group multiple threads in a single object. In such a way, we can suspend, resume or interrupt a group of threads by a single method call.
Note: Now suspend(), resume() and stop() methods are deprecated.
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.
Constructors of ThreadGroup class
There are only two constructors of ThreadGroup class.
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 a given parent group and name. |
Methods of ThreadGroup class
There are many methods in ThreadGroup class. A list of ThreadGroup methods is given below.
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 group is a daemon thread group. |
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.
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.
ThreadGroup Example
File: ThreadGroupDemo.java
Output:
one two three Thread Group Name: Parent ThreadGroup java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
Thread Pool Methods Example: int activeCount()
Let’s see how one can use the method activeCount().
FileName: ActiveCountExample.java
Output:
Starting the first Starting the second The total number of active threads are: 2
Thread Pool Methods Example: int activeGroupCount()
Now, we will learn how one can use the activeGroupCount() method in the code.
FileName: ActiveGroupCountExample.java
Output:
Starting the first Starting the second The total number of active thread groups are: 1 the second thread has finished executing the first thread has finished executing
Thread Pool Methods Example: void destroy()
Now, we will learn how one can use the destroy() method in the code.
FileName: DestroyExample.java
Output:
Starting the first Starting the second the first thread has finished executing the second thread has finished executing the child group is destroyed. the parent group is destroyed.
Thread Pool Methods Example: int enumerate()
Now, we will learn how one can use the enumerate() method in the code.
FileName: EnumerateExample.java
Output:
Starting the first Starting the second Thread the first is found. Thread the second is found. the first thread has finished executing the second thread has finished executing
Thread Pool Methods Example: int getMaxPriority()
The following code shows the working of the getMaxPriority() method.
FileName: GetMaxPriorityExample.java
Output:
Starting the first Starting the second The maximum priority of the parent ThreadGroup: 10 the first thread has finished executing the second thread has finished executing
Thread Pool Methods Example: ThreadGroup getParent()
Now, we will learn how one can use the getParent() method in the code.
FileName: GetParentExample.java
Output:
Starting the first Starting the second The ParentThreadGroup for the parent group is main The ParentThreadGroup for the child group is the parent group the first thread has finished executing the second thread has finished executing
Thread Pool Methods Example: void interrupt()
The following program illustrates how one can use the interrupt() method.
FileName: InterruptExample.java
Output:
Starting the first Starting the second The exception has been encountered java.lang.InterruptedException: sleep interrupted The exception has been encountered java.lang.InterruptedException: sleep interrupted the second thread has finished executing the first thread has finished executing
Thread Pool Methods Example: boolean isDaemon()
The following program illustrates how one can use the isDaemon() method.
FileName: IsDaemonExample.java
Output:
Starting the first Starting the second The group is not a daemon group. the second thread has finished executing the first thread has finished executing
Thread Pool Methods Example: boolean isDestroyed()
The following program illustrates how one can use the isDestroyed() method.
FileName: IsDestroyedExample.java
Output:
Starting the first Starting the second The group has not been destroyed. the first thread has finished executing the second thread has finished executing