0% found this document useful (0 votes)
3 views3 pages

ThreadGroup in Java

Java's ThreadGroup class allows for the grouping of multiple threads, enabling operations like suspension and interruption through single method calls. The class provides constructors for creating thread groups and various methods to manage and retrieve information about the threads within the group. An example code demonstrates how to create a thread group and manage its threads effectively.

Uploaded by

jitprosen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

ThreadGroup in Java

Java's ThreadGroup class allows for the grouping of multiple threads, enabling operations like suspension and interruption through single method calls. The class provides constructors for creating thread groups and various methods to manage and retrieve information about the threads within the group. An example code demonstrates how to create a thread group and manage its threads effectively.

Uploaded by

jitprosen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

Java thread group is implemented by [Link] 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.

Constructor Description

ThreadGroup (String name) creates a thread group with given name.

ThreadGroup (ThreadGroup parent, creates a thread group with a given parent group and name.
String name)

Methods of ThreadGroup class


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

Return Type Method Description

void checkAccess() This method determines if the currently running thread


has permission to modify the thread group.

int activeCount() This method returns an estimate of the number of


active threads in the thread group and its subgroups.

int activeGroupCount() This method returns an estimate of the number


of active groups in the thread group and its subgroups.

void destroy() This method destroys the thread group and


all of its subgroups.

int enumerate(Thread[] list) This method copies into the specified array every
active thread in the thread group and its subgroups.
int getMaxPriority() This method returns the maximum priority of the
thread group.

String getName() This method returns the name of the thread group.

ThreadGroup getParent() This method returns the parent of the thread group.

void interrupt() This method interrupts all threads in the thread group.

boolean isDaemon() This method tests if the thread group is a daemon


thread group.

void setDaemon(boolean This method changes the daemon status of the


daemon) thread group.

boolean isDestroyed() This method tests if this thread group has been
destroyed.

void list() This method prints information about the thread


group to the standard output.

boolean parentOf(ThreadGroup g) This method tests if the thread group is either the thread
group argument or one of its ancestor thread groups.

void suspend() This method is used to suspend all threads in the


thread group.

void resume() This method is used to resume all threads in the thread
group which was suspended using suspend() method.

void setMaxPriority(int pri) This method sets the maximum priority of the group.

void stop() This method is used to stop all threads in the thread
group.

String toString() This method returns a string representation of the


Thread group.

Let's see a code to group multiple threads.

1. ThreadGroup tg1 = new ThreadGroup("Group A");


2. Thread t1 = new Thread(tg1, new MyRunnable(), "one");
3. Thread t2 = new Thread(tg1, new MyRunnable(), "two");
4. Thread t3 = new Thread(tg1, new MyRunnable(), "three");

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

[Link]().getThreadGroup().interrupt();

ThreadGroup Example

public class ThreadGroupTest implements Runnable{

public void run() {


[Link]([Link]().getName());
}
public static void main(String[] args) {
ThreadGroupTest tgt = new ThreadGroupTest();
ThreadGroup tg1 = new ThreadGroup("Footballer");

Thread t1 = new Thread(tg1, tgt,"Messi");


[Link]();
Thread t2 = new Thread(tg1, tgt,"Ronaldo");
[Link]();
Thread t3 = new Thread(tg1, tgt,"Neymar");
[Link]();

[Link]("Thread Group Name: "+[Link]());


[Link](); // to list out the details of all threads under
// the thread group instance referred to by tg1
[Link]("End of main thread");

}
}

Output:

You might also like