Thread Group

Thread Group

A class which is used to create a group of threads is known as thread group. This group of threads looks like a tree structure, in which the first thread is the parent thread. A thread consists data of the other threads in the groups. In scenarios where we want to suspend and resume numbers of threads, thread group is useful. It is implemented by java.lang.ThreadGroup class.

Some methods which can be used are listed below:

  • int activeCount( )
  • int activeGroupCount( )
  • void checkAccess( ) 
  • void destroy( )
  • int enumerate(Thread[ ] list)
  • int enumerate(ThreadGroup[] list)
  • String getName( )
  • int getMaxPriority( )
  • void interrupt( )
  • String toString( )

ThreadGroup in java refers to a function that generates a group of threads. It provides a simple approach for managing the thread groups as a whole. This is very useful when you need to pause and restart a lot of connected threads.

  • The thread groups form a tree with every thread group having a parent except for the first.
  • A thread can only access information about its thread group; it cannot access information about its parent thread group or any other thread group.

Constructors of thread group in java:

  • public ThreadGroup(String name): It creates a new thread group. The thread group of the presently running thread is the new group's parent.
  • public ThreadGroup(ThreadGroup parent, String name): It creates a new thread group. The specified thread group is the new group's parent.


Com.knowledge2life public class Main implements Runnable{ public void run() { System.out.println(Thread.currentThread().getName()); } public static void main(String[] args) { Main runnable = new Main(); ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup"); Thread t1 = new Thread(tg1, runnable,"Knowledge2life 1"); t1.start(); Thread t2 = new Thread(tg1, runnable,"Knowledge2life 2"); t2.start(); Thread t3 = new Thread(tg1, runnable,"Knowledge2life 3"); t3.start(); System.out.println("Thread Group Name: "+tg1.getName()); tg1.list(); } }


Knowledge2life 1
Knowledge2life 2
Knowledge2life 3
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]