In this article we will learn what is a thread group? How to work with thread groups in Java along with example program.
A thread group is a collection of threads. A thread group allows the programmer to maintain a group of threads more effectively. To support thread groups Java provides a class named ThreadGroup available in java.lang package.
Some of the constructors available in ThreadGroup class are:
ThreadGroup(String group-name)
ThreadGroup(ThreadGroup parent, String group-name)
After creating a thread group using one of the above constructors, we can add a thread to the thread group using one of the following Thread constructors:
Thread(ThreadGroup ref, Runnable obj)
Thread(ThreadGroup ref, String thread-name)
Thread(ThreadGroup ref, Runnable obj, String thread-name)
Following are some of the methods available in ThreadGroup class:
- getName() – To get the name of the thread group
- setMaxPriority() – To set the maximum priority of all the threads in the group
- setMinPriority() – To set the minimum priority of all the threads in the group
- start() – To start the execution of all the threads in the group
- list() – To print information of the thread group and the threads in the group.
Below program demonstrates the use of thread groups in Java:
class ChildThread implements Runnable
{
Thread t;
ChildThread(ThreadGroup g, String name)
{
t = new Thread(g,this,name);
g.setMaxPriority(8);
System.out.println(t.getName()+" belongs to the group: "+g.getName());
System.out.println("Maximum priority of "+g.getName()+" is: "+g.getMaxPriority());
t.start();
}
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
System.out.println(t.getName()+":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(t.getName()+" is interrupted");
}
}
}
public class Driver
{
public static void main(String[] args) throws InterruptedException
{
ThreadGroup tg1 = new ThreadGroup("Group A");
ChildThread one = new ChildThread(tg1, "First Thread");
ChildThread two = new ChildThread(tg1, "Second Thread");
try
{
Thread.sleep(3000);
System.out.println("All the threads in the group will be stopped");
tg1.stop();
}
catch(InterruptedException e)
{
System.out.println("main thread is interrupted");
}
tg1.list();
}
}
Output of the above program is:
First Thread belongs to the group: Group A
Maximum priority of Group A is: 8
Second Thread belongs to the group: Group A
Maximum priority of Group A is: 8
First Thread:1
Second Thread:1
First Thread:2
Second Thread:2
Second Thread:3
First Thread:3
All the threads in the group will be stopped
java.lang.ThreadGroup[name=Group A,maxpri=8]
Thread[First Thread,5,Group A]
Thread[Second Thread,5,Group A]
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply