A group of threads is created by ThreadGroup class. It provides a practical method for controlling thread groups collectively. This is especially useful when you need to pause and resume a number of connected threads.
Every thread group, excluding the initial thread group, has a parent in the thread group tree.
A thread is permitted to access data about its own thread group, but it is not permitted to access data about any other thread group or its parent thread group.
The ThreadGroup class has just two constructors.
The ThreadGroup class has numerous methods. The following are some ThreadGroup methods-
The number of threads in the group, plus any groups for which this thread is the parent, are returned by the int activeCount() method.
Syntax:
public static int activeCount()
// import statement
import java.lang.*;
class ThreadNew extends Thread
{
// constructor of the class
ThreadNew(String tName, ThreadGroup tgrp)
{
super(tgrp, tName);
start();
}
// overriding the run method
public void run()
{
for (int j = 0; j < 1000; j++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
System.out.println("The exception has been encountered " + e);
}
}
}
}
public class ActiveCountExample
{
// main method
public static void main(String argvs[])
{
// creating the thread group
ThreadGroup tg = new ThreadGroup("The parent group of threads");
ThreadNew th1 = new ThreadNew("first", tg);
System.out.println("Starting the first");
ThreadNew th2 = new ThreadNew("second", tg);
System.out.println("Starting the second");
// checking the number of active thread by invoking the activeCount() method
System.out.println("The total number of active threads are: " + tg.activeCount());
}
}
Starting the first
Starting the second
The total number of active threads are: 2
An estimate of the number of active groups in this thread group is returned by this method.
Syntax:
public int activeGroupCount()
// import statement
import java.lang.*;
class ThreadNew extends Thread
{
// constructor of the class
ThreadNew(String tName, ThreadGroup tgrp)
{
super(tgrp, tName);
start();
}
// overriding the run() method
public void run()
{
for (int j = 0; j < 100; j++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
System.out.println("The exception has been encountered " + e);
}
}
System.out.println(Thread.currentThread().getName() + " thread has finished executing");
}
}
public class ActiveGroupCountExample
{
// main method
public static void main(String argvs[])
{
// creating the thread group
ThreadGroup tg = new ThreadGroup("The parent group of threads");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
ThreadNew th1 = new ThreadNew("the first", tg);
System.out.println("Starting the first");
ThreadNew th2 = new ThreadNew("the second", tg);
System.out.println("Starting the second");
// checking the number of active thread by invoking the activeGroupCount() method
System.out.println("The total number of active thread groups are: " + tg.activeGroupCount());
}
}
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
Causes the security manager to check that the thread calling checkAccess() has permission to access and/or modify the group.
Syntax:
final void checkAccess()
// Java code illustrating checkAccess() method
import java.lang.*;
class NewThread extends Thread
{
NewThread(String threadname, ThreadGroup tgob)
{
super(tgob, threadname);
start();
}
public void run()
{
for (int i = 0; i < 1000; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println("Exception encounterted");
}
}
System.out.println(Thread.currentThread().getName() +
" finished executing");
}
}
public class ThreadGroupDemo
{
public static void main(String arg[]) throws InterruptedException,
SecurityException
{
// creating the thread group
ThreadGroup tobj = new ThreadGroup("Parent thread");
ThreadGroup tobj_child = new ThreadGroup(tobj, "child thread");
NewThread t1 = new NewThread("one", tobj);
System.out.println("Starting one");
NewThread t2 = new NewThread("two", tobj);
System.out.println("Starting two");
tobj.checkAccess();
System.out.println(tobj.getName() + " has access");
tobj_child.checkAccess();
System.out.println(tobj_child.getName() + " has access");
}
}
Starting one
Starting two
Parent thread has access
child thread has access
one finished executing
two finished executing
The thread group and any offspring on which it was called are destroyed.
Syntax:
public void destroy().
// import statement
import java.lang.*;
class ThreadNew extends Thread
{
// constructor of the class
ThreadNew(String tName, ThreadGroup tgrp)
{
super(tgrp, tName);
start();
}
// overriding the run() method
public void run()
{
for (int j = 0; j < 100; j++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
System.out.println("The exception has been encountered " + e);
}
}
System.out.println(Thread.currentThread().getName() + " thread has finished executing");
}
}
public class DestroyExample
{
// main method
public static void main(String argvs[]) throws SecurityException, InterruptedException
{
// creating the thread group
ThreadGroup tg = new ThreadGroup("the parent group");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
ThreadNew th1 = new ThreadNew("the first", tg);
System.out.println("Starting the first");
ThreadNew th2 = new ThreadNew("the second", tg);
System.out.println("Starting the second");
// waiting until the other threads has been finished
th1.join();
th2.join();
// destroying the child thread group
tg1.destroy();
System.out.println(tg1.getName() + " is destroyed.");
// destroying the parent thread group
tg.destroy();
System.out.println(tg.getName() + " is destroyed.");
}
}
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.
The group array contains the thread that makes up the invoking thread group.
Syntax:
public int enumerate(Thread group[])
// import statement
import java.lang.*;
class ThreadNew extends Thread
{
// constructor of the class
ThreadNew(String tName, ThreadGroup tgrp)
{
super(tgrp, tName);
start();
}
// overriding the run() method
public void run()
{
for (int j = 0; j < 100; j++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
System.out.println("The exception has been encountered " + e);
}
}
System.out.println(Thread.currentThread().getName() + " thread has finished executing");
}
}
public class EnumerateExample
{
// main method
public static void main(String argvs[]) throws SecurityException, InterruptedException
{
// creating the thread group
ThreadGroup tg = new ThreadGroup("the parent group");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
ThreadNew th1 = new ThreadNew("the first", tg);
System.out.println("Starting the first");
ThreadNew th2 = new ThreadNew("the second", tg);
System.out.println("Starting the second");
// returning the number of threads kept in this array
Thread[] grp = new Thread[tg.activeCount()];
int cnt = tg.enumerate(grp);
for (int j = 0; j < cnt; j++)
{
System.out.println("Thread " + grp[j].getName() + " is found.");
}
}
}
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
Returns the maximum priority setting for the group.
Syntax:
final int getMaxPriority()
import java.lang.*;
class ThreadNew extends Thread
{
// constructor of the class
ThreadNew(String tName, ThreadGroup tgrp)
{
super(tgrp, tName);
start();
}
// overriding the run() method
public void run()
{
for (int j = 0; j < 100; j++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
System.out.println("The exception has been encountered " + e);
}
}
System.out.println(Thread.currentThread().getName() + " thread has finished executing");
}
}
public class GetMaxPriorityExample
{
// main method
public static void main(String argvs[]) throws SecurityException, InterruptedException
{
// creating the thread group
ThreadGroup tg = new ThreadGroup("the parent group");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
ThreadNew th1 = new ThreadNew("the first", tg);
System.out.println("Starting the first");
ThreadNew th2 = new ThreadNew("the second", tg);
System.out.println("Starting the second");
int priority = tg.getMaxPriority();
System.out.println("The maximum priority of the parent ThreadGroup: " + priority);
}
}
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
Invokes the interrupt() methods of all threads in the group.
Syntax:
public final void interrupt().
import java.lang.*;
class ThreadNew extends Thread
{
// constructor of the class
ThreadNew(String tName, ThreadGroup tgrp)
{
super(tgrp, tName);
start();
}
// overriding the run() method
public void run()
{
for (int j = 0; j < 100; j++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
System.out.println("The exception has been encountered " + e);
}
}
System.out.println(Thread.currentThread().getName() + " thread has finished executing");
}
}
public class InterruptExample
{
// main method
public static void main(String argvs[]) throws SecurityException, InterruptedException
{
// creating the thread group
ThreadGroup tg = new ThreadGroup("the parent group");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
ThreadNew th1 = new ThreadNew("the first", tg);
System.out.println("Starting the first");
ThreadNew th2 = new ThreadNew("the second", tg);
System.out.println("Starting the second");
// invoking the interrupt method
tg.interrupt();
}
}
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
determines whether the thread group is a daemon thread group. When a daemon thread group’s final thread or thread group is terminated, a daemon thread group is also automatically destroyed.
Syntax:
public final boolean isDaemon().
import java.lang.*;
class ThreadNew extends Thread
{
// constructor of the class
ThreadNew(String tn, ThreadGroup grp)
{
super(grp, tn);
start();
}
// overriding the run() method
public void run()
{
for (int j = 0; j < 100; j++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
System.out.println("The exception is encountered" + e);
}
}
System.out.println(Thread.currentThread().getName() + " thread has finished executing");
}
}
public class IsDaemonExample
{
// main method
public static void main(String argvs[]) throws SecurityException, InterruptedException
{
// creating the thread group
ThreadGroup tg = new ThreadGroup("the parent group");
ThreadGroup tg1 = new ThreadGroup(tg, "the child group");
ThreadNew th1 = new ThreadNew("the first", tg);
System.out.println("Starting the first");
ThreadNew th2 = new ThreadNew("the second", tg);
System.out.println("Starting the second");
if (tg.isDaemon() == true)
{
System.out.println("The group is a daemon group.");
}
else
{
System.out.println("The group is not a daemon group.");
}
}
}
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
Note: also read about the Inter-thread Communication in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…