Threadgroup In Java

In Java, there is a convenient method to organize numerous threads within a single entity. This allows for the collective suspension, resumption, or interruption of a group of threads through a singular method invocation.

Note: Now suspend, resume and stop methods are deprecated.

The Java thread group is created using the java.lang.ThreadGroup class.

A ThreadGroup denotes a collection of threads. It is possible for a thread group to encompass another thread group within it. This results in the creation of a hierarchical structure where each thread group, with the exception of the root thread group, is linked to a parent thread group.

A thread has the permission to retrieve details regarding its own thread group; however, it is restricted from accessing details about the parent thread group of its thread group or any other thread groups.

Constructors of ThreadGroup class

The ThreadGroup class has a total of two constructors available for instantiation.

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

The ThreadGroup class offers various techniques through its methods. Below is a compilation of ThreadGroup methods.

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.

Example

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

Thread t1 = new Thread(tg1,new MyRunnable(),"one");   

Thread t2 = new Thread(tg1,new MyRunnable(),"two");   

Thread t3 = new Thread(tg1,new MyRunnable(),"three");

All three threads are now part of a single group named tg1. The class MyRunnable implements the Runnable interface, and the threads are named "one," "two," and "three."

Now it is possible to halt all threads with just one line of code.

Example

Thread.currentThread().getThreadGroup().interrupt();

ThreadGroup Example

File: ThreadGroupDemo.java

Example

public class ThreadGroupDemo implements Runnable{  

    public void run() {  

          System.out.println(Thread.currentThread().getName());  

    }  

   public static void main(String[] args) {  

      ThreadGroupDemo runnable = new ThreadGroupDemo();  

          ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup");  

            

          Thread t1 = new Thread(tg1, runnable,"one");  

          t1.start();  

          Thread t2 = new Thread(tg1, runnable,"two");  

          t2.start();  

          Thread t3 = new Thread(tg1, runnable,"three");  

          t3.start();  

               

          System.out.println("Thread Group Name: "+tg1.getName());  

         tg1.list();  

  

    }  

   }

Output:

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 explore how to utilize the activeCount method.

FileName: ActiveCountExample.java

Example

// code that illustrates the activeCount() method



// 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:

Output

Starting the first

Starting the second

The total number of active threads are: 2

Thread Pool Methods Example: int activeGroupCount

Next, we'll explore the utilization of the activeGroupCount function in our code implementation.

FileName: ActiveGroupCountExample.java

Example

// Java code illustrating the activeGroupCount() method



// 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:

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

Next, we will explore the utilization of the destroy method within the codebase.

FileName: DestroyExample.java

Example

// Code illustrating the destroy() method



// 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:

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

Next, we will explore the utilization of the enumerate function within code implementations.

FileName: EnumerateExample.java

Example

// Code illustrating the enumerate() method



// 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:

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

Displayed below is an example of how the getMaxPriority method operates.

FileName: GetMaxPriorityExample.java

Example

// Code illustrating the getMaxPriority() method



// 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 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:

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

Next, we'll explore the utilization of the getParent function in the code implementation.

FileName: GetParentExample.java

Example

// Code illustrating the getParent() method



// 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 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");



// printing the parent ThreadGroup 

// of both child and parent threads

System.out.println("The ParentThreadGroup for " + tg.getName() + " is " + tg.getParent().getName());

System.out.println("The ParentThreadGroup for " + tg1.getName() + " is " + tg1.getParent().getName());





}

}

Output:

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 upcoming code example demonstrates the utilization of the interrupt method.

FileName: InterruptExample.java

Example

// Code illustrating the interrupt() method



// 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 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:

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 upcoming example demonstrates the utilization of the isDaemon function in a program.

FileName: IsDaemonExample.java

Example

// Code illustrating the isDaemon() method



// 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 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:

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 upcoming example demonstrates the utilization of the isDestroyed function.

FileName: IsDestroyedExample.java

Example

// Code illustrating the isDestroyed() method



// 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 IsDestroyedExample 

{

// 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.isDestroyed() == true)

{

System.out.println("The group has been destroyed.");

}

else

{

System.out.println("The group has not been destroyed.");

}



}

}

Output:

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

Input Required

This code uses input(). Please provide values below: