Thread Group

  1. Based on functionality we can group threads into a single unit which is nothing but thread group i.e Thread group contains a group of Threads.
  2. In addition to threads, thread group can also contain sub thread groups
  3. The main advantage of maintaining Thread in the form of Thread group is we can perform common operations very easily.
  4. Every thread in java belongs some group, main thread belongs to main group.
  5. Every thread group in java is a child group of system group either directly or indirectly. Hence system group acts as root for all thread groups in java.
  6. System group contains several system level threads like :
    a. Finalizer (Garbage Collector)
    b. Reference Handler
    c. Signal Dispatcher
    d. Attach Listener
    e. Main Thread group
    etc
  7. ThreadGroup is a java class present in java.lang package and it is direct child class of object.
  8. Threads in the Thread Group that already have higher priority won’t be affected when a priority of their Thread Group changes(setMaxPriority()) but for newly added Threads this priority is applicable.
public class ThreadGroupDemo {
	public static void main(String[] args) {
		System.out.println(Thread.currentThread().getThreadGroup().getName());
		System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
	}
}

Output :

Constructors :

  1. ThreadGroup g = new ThreadGroup(String group);
    Creates a new Thread group with the specified group name.
    The parent of the new group is the thread group of currently executing thread.
    Example : ThreadGroup g = new ThreadGroup(“First Group”);
  2. ThreadGroup g = new ThreadGroup(ThreadGroup parentGroup,String groupName);
    Creates a new Thread group with specified group name.
    The parent of this new thread group is specified parent group.
    Example : ThreadGroup g1 = new ThreadGroup(g,”Second Group”);
ThreadGroup g = new ThreadGroup(String group);
ThreadGroup g = new ThreadGroup(ThreadGroup parentGroup,String groupName);

public class ThreadGroupDemo {
	public static void main(String[] args) {
		ThreadGroup g = new ThreadGroup("FirstGroup");
		System.out.println(g.getParent().getName());
		ThreadGroup g1 = new ThreadGroup(g,"SecondGroup");
		System.out.println(g1.getParent().getName());
	}
}

Output

system group --> main group --> First Group --> Second Group

Important Method of Thread Group Class :

Sting getName()
int getMaxPriority()
void setMaxPriority(int p)
ThreadGroup getParent()
void list() //print information about thread group
int activeCount() //returns number of active threads present in the Thread group
int activeCountGroup() //returns number of active group present in the current thread group
int enumeration(Thread[] t) //to copy all active Threads(also sub threads) of this thread group into array
int enumeration(ThreadGroup[] t) //To copy all active sub Thread Groups into thread group array
boolean isDaemon() //To check if a the ThreadGroup is a Deamon group 
void setDaemon(boolean b) 
void interrupt() //To interrupt all waiting or sleeping thread present in the Thread Group 
void destroy() //To destroy ThreadGroup and its sub thread groups
  1. Threads in the Thread Group that already have higher priority won’t be affected when a priority of their Thread Group changes(setMaxPriority()) but for newly added Threads this priority is applicable.
public class ThreadGroupDemo2 {
	public static void main(String[] args) {
		ThreadGroup g1 = new ThreadGroup("Group 1");
		Thread t1 = new Thread(g1,"Thread 1");
		Thread t2 = new Thread(g1,"Thread 2");
		g1.setMaxPriority(3);
		Thread t3 = new Thread(g1,"Thread 3");
		System.out.println(t1.getPriority());//5
		System.out.println(t2.getPriority());//5
		System.out.println(t3.getPriority());//3
	}
}

2. activeCount() and activeGroupCount() and list()

package com.demo8;

public class ThreadGroupDemo3 {
	public static void main(String[] args) throws InterruptedException {
		ThreadGroup g1 = new ThreadGroup("Group 1");
		ThreadGroup g2 = new ThreadGroup(g1,"Group 2");
		MyThread t1 = new MyThread(g1,"Thread 1");
		MyThread t2 = new MyThread(g1,"Thread 2");
		t1.start();
		t2.start();
		System.out.println("Active Thread count : "+g1.activeCount());
		System.out.println("Active Thread count : "+g1.activeGroupCount());
		g1.list();
		t1.join();
		t2.join();
		System.out.println("Active Thread count : "+g1.activeCount());
		System.out.println("Active Thread count : "+g1.activeGroupCount());
		g1.list();
	}
}
class MyThread extends Thread{
	MyThread(ThreadGroup groupName,String name){
		super(groupName,name);
	}
	public void run() {
		System.out.println("Child Thread");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

Output :

Thread Group hierarchy of the above program : 
system group --> main group --> group 1 --> group 2

3. int enumeration(Thread[] t) and int enumeration(ThreadGroup[] t)
Write a program to display all active thread names belonging to system group and its child groups.

public class ThreadGroupDemo4 {
	public static void main(String[] args) {
		ThreadGroup systemGroup = Thread.currentThread().getThreadGroup().getParent();
		Thread[] threadArray = new Thread[systemGroup.activeCount()];
		systemGroup.enumerate(threadArray);
		for(Thread t : threadArray) {
			System.out.println("Thread Name : "+t.getName()+" -----------> Is Demon : "+t.isDaemon());
		}
	}
}

Leave a Comment