Java Thread Group

In the complex environment, multiple threads exists in the system. In order to manage all the threads becomes very much complicated task in the thread management.

Grouping the Threads :

  1. To simplify thread management, threads in program are organized in group called thread groups
  2. java.lang.ThreadGroup class is used to manage threads
  3. Each thread is created under the system group. User created threads are under any of the thread group.
  4. System group organizes all the threads related to JVM which are useful for system relayed task.

Thread Group

Create thread groups and associate threads with groups :

We have implemented the same structure that we have seen in the above diagram using below code.

package com.c4learn.thread;
public class ThreadGroupDemo {
 public static void main (String [] args)
 {
   ThreadGroup tg = new ThreadGroup ("subgroup 1");
   Thread t1 = new Thread (tg, "thread 1");
   Thread t2 = new Thread (tg, "thread 2");
   Thread t3 = new Thread (tg, "thread 3");
   tg = new ThreadGroup ("subgroup 2");
   Thread t4 = new Thread (tg, "thread 4");
   Thread t5 = new Thread (tg, "thread 5");
   tg = Thread.currentThread ().getThreadGroup();
   int count = tg.activeGroupCount();
   System.out.println("Parent of Thread Groups : " + 
			tg.getParent().getName());
   System.out.println("Active Thread Groups : " + 
                        tg.getName());
   System.out.println("Thread Groups Count : " + count);
   tg.list ();
   }
}

Output :

Parent of Thread Groups : system
Active Thread Groups : main
Thread Groups Count : 2
java.lang.ThreadGroup[name=main,maxpri=10]
    Thread[main,5,main]
    java.lang.ThreadGroup[name=subgroup 1,maxpri=10]
    java.lang.ThreadGroup[name=subgroup 2,maxpri=10]

Explanation of Program :

We have created object of ThreadGroup class which is used to constructs a new thread group. The parent of this new group is the thread group of the currently running thread.

In the above program we have created one thread group using below syntax -

ThreadGroup tg = new ThreadGroup ("subgroup 1");
Thread t1 = new Thread (tg, "thread 1");
Thread t2 = new Thread (tg, "thread 2");
Thread t3 = new Thread (tg, "thread 3");

We have added thread t1,t2,t3 in the subgroup1.

Now we have again constructed another thread group - subgroup2

tg = new ThreadGroup ("subgroup 2");
Thread t4 = new Thread (tg, "thread 4");
Thread t5 = new Thread (tg, "thread 5");

Printing the results :

getThreadGroup() method returns the thread group to which currently running thread belongs. Method returns null if this thread has died or stopped.

tg = Thread.currentThread ().getThreadGroup();

activeGroupCount() method returns an estimate of the number of active groups in this thread group and its subgroups.

It recursively iterates over all subgroups in current thread group.

int count = tg.activeGroupCount();

In order to print information about this thread group to the standard output list() method is called.

tg.list ();