Categories: Java

Java.lang.ThreadGroup- class in Java

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.

Constructors for the class ThreadGroup:

The ThreadGroup class has just two constructors.

  • ThreadGroup(String name)– builds a thread group with the specified name.
  • ThreadGroup(ThreadGroup parent, String name)– creates a thread group with the specified name and parent group.
Methods of the class ThreadGroup:

The ThreadGroup class has numerous methods. The following are some ThreadGroup methods-

1)int activeCount():

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()  
Example:
 // 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());  
}  
}  
Output:
Starting the first
Starting the second
The total number of active threads are: 2
2)int activeGroupCount():

An estimate of the number of active groups in this thread group is returned by this method.

Syntax:

public int activeGroupCount()
Example:
  
// 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());  
}  
}
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
3)void checkAccess():

Causes the security manager to check that the thread calling checkAccess() has permission to access and/or modify the group.

Syntax:

final void checkAccess()
Example:
// 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");
 }
}
Output:
Starting one
Starting two
Parent thread has access
child thread has access
one finished executing 
two finished executing
4)void destroy():

The thread group and any offspring on which it was called are destroyed.

Syntax:

public void destroy().
Example:
// 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.");  
}  
}
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.
5)int enumerate (Thread group[]):

The group array contains the thread that makes up the invoking thread group.

Syntax:

public int enumerate(Thread group[])
Example:
// 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.");  
}  
}  
}  
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
6)int getMaxPriority():

Returns the maximum priority setting for the group.

Syntax:

final int getMaxPriority()
Example:
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);  
  
  
}  
}  
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
7)void interrupt():

Invokes the interrupt() methods of all threads in the group.

Syntax: 
public final void interrupt().
Example:
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();  
  
}  
}  
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
8)boolean isDaemon():

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().
Example:
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.");  
}  
  
}  
}  
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

Note: also read about the Inter-thread Communication in Java

Follow Me

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

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago