ThreadGroup Grouping threads


Introduction to thread group and grouping of threads

We know earlier, every thread should belong to some thread group and if no group is mentioned, the thread is placed, by default, in main thread group. When the JVM starts a new process, it creates main thread group and every thread created by the programmer, if not mentioned any group, the JVM keeps the thread in the main thread group. The programmer can explicitly mention a thread group also where the thread is to be placed. But remember, the thread should be placed in a group at the time of creating the thread only; else it is placed in main thread group implicilty. Once a thread is allotted a thread group, throughout its life, the group cannot be changed.

By placing the thread in a group, the advantage is, the thread attains the properties of the group implicitly. For example, any thread belonging to main thread group attains a default priority of 5 because main group priority is set to 5. The java.lang package includes ThreadGroup class to manipulate the threads with groups.

Placing Threads in ThreadGroup

With the above knowledge of ThreadGroup Grouping of threads, now let us write a program where a thread is created, placed in a group and its properties are set explicitly.

public class ThreadGroupInfo
{
  public static void main(String args[])
  {
    ThreadGroup tg1 = new ThreadGroup("FinanceGroup");

    System.out.println("tg1 name: " + tg1.getName());                                 
    System.out.println("tg1 parent group: " + tg1.getParent());         
    System.out.println("tg1 priority, by default: " + tg1.getMaxPriority());     
    tg1.setMaxPriority(Thread.NORM_PRIORITY+3);      
    System.out.println("tg1 priority after setting: " + tg1.getMaxPriority());     
    System.out.println("Active threads in the tg1 group: " + tg1.activeCount());           
                                                                                  
    Thread t1= new Thread(tg1,"RevenueThread");
    System.out.println("Thread t1 Name:  " + t1.getName());     
    System.out.println("t1 group: " + t1.getThreadGroup());
  }
}

ThreadGroup Grouping threads
Let us discuss some methods of ThreadGroup used in the above program.

ThreadGroup tg1 = new ThreadGroup(“FinanceGroup”);

A thread group by name FinanceGroup is created. The programmer can retrieve the name of the group with getName() method. getParent() method returns the parent thread group of FinanceGroup. Any group created by the programmer is placed in main thread group by the JVM implicitly. This is how the JVM controls the user-defined thread groups. getMaxPriority() returns the maximum priority, the threads of a group can be assigned. Observe the screenshot, even though the default priority of main group is set to 5 implicitly by JVM, the programmer can assign to a maximum of 10 to a thread. Using setMaxPriority() method, the programmer can assign a priority to a group. In the program it is set to 8 (5+3). The activeCount() method returns the number of active threads in the group. It printed zero in the program as we did not place any threads in the group and started.

Thread t1= new Thread(tg1,"RevenueThread");

In the above statement, a thread by name t1 is created with name RevenueThread and is placed in the group tg1. It must be observed that a thread should be placed in a group at the time of creation itself; else, it is placed in main group and once placed in a group, it cannot be changed.

Advantages of ThreadGroup Grouping threads

It is learnt earlier that every thread should belong to a group. With thread group, all the threads in a group can be assigned properties at a time instead of the laborious way of setting property to each individual thread separately. Following are the benefits with thread groups.

  • All the threads can be given a common priority. For example, the common priority set to all the threads of main group is 5.
  • All the threads of a group can be stopped, suspended or resumed at a time, but cannot be started at a time ; each individual thread should be started separately.

Generally, thread groups are used very less in programming. The programmer leaves the thread management to the JVM itself. SecurityManager class should be used to have extra control or manipulation over the thread groups.

Leave a Comment

Your email address will not be published.