ThreadGroup in java


Contents of page :

  • main ThreadGroup in java
  • When Program starts JVM creates  a ThreadGroup named main
  • ThreadGroup’s  constructor >
  • ThreadGroup(String name)
  • ThreadGroup(ThreadGroup parent, String name)
      • Program to demonstrate ThreadGroup(ThreadGroup parent, String name) constructor >

  • ThreadGroup important methods >
  • getName()
  • activeGroupCount()
  • activeCount()
  • list()
  • getMaxPriority()
  • setMaxPriority(int pri)


  • ThreadGroup and thread priorities >
    • What is default priority of newly created threadGroup?
  • Program to demonstrate ThreadGroup and thread priorities >
  • OUTPUT ANALYZATION of ThreadGroup and thread priorities program >


main ThreadGroup in java

When program starts JVM creates  a ThreadGroup named main. Unless specified, all  newly created threads become members of the main thread group.

When Program starts JVM creates  a ThreadGroup named main
package threadGroupTest0_groupName;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ThreadGroupTest {
   public static void main(String[] args) {
          //When program starts JVM creates a ThreadGroup named main
          System.out.println("current ThreadGroup name (created by JVM) = "
                       +Thread.currentThread().getThreadGroup().getName());
         
          /* Unless specified, all  newly created threads
          * become members of the main ThreadGroup.
          */
          Thread thread1 = new Thread("Thread-1");
          Thread thread2 = new Thread("thread-2");
          System.out.println("Thread-1's ThreadGroup = "
                        +thread1.getThreadGroup().getName());
          System.out.println("Thread-2's ThreadGroup = "
                        +thread2.getThreadGroup().getName());
         
   }
}
/*OUTPUT
current ThreadGroup name (created by JVM) = main
Thread-1's ThreadGroup = main
Thread-2's ThreadGroup = main
*/

When program starts JVM creates a ThreadGroup named main.
So, Thread.currentThread().getThreadGroup().getName() returned main.
We created Thread-1 and Thread-2, no ThreadGroup was specified so both threads became member of the main thread group.



ThreadGroup’s  constructor >
  • ThreadGroup(String name)
Creates a new thread group with specified name.
The parent of this ThreadGroup is ThreadGroup of the currently running thread.

In the program in below section, we will be creating ThreadGroup named childThreadGroup1.
ThreadGroup childThreadGroup1 = new ThreadGroup("childThreadGroup1");         


The parent of this childThreadGroup1 is ThreadGroup of the currently running thread i.e. main. [When program starts JVM creates a ThreadGroup named main and thread named main, where main thread belongs to main ThreadGroup]

Likewise, the parent of childThreadGroup2 is also main ThreadGroup.

  • ThreadGroup(ThreadGroup parent, String name)
Creates a new thread group with specified name.
The parent of this ThreadGroup is the specified parent ThreadGroup.

Program to demonstrate ThreadGroup(ThreadGroup parent, String name) constructor >
public class ThreadGroupTest {
public static void main(String[] args) {
 ThreadGroup childThreadGroup = new ThreadGroup("childThreadGroup");
 ThreadGroup childsChildThreadGroup = new ThreadGroup(childThreadGroup,                                                                                                                       "childsChildThreadGroup");    

 System.out.println("childThreadGroup's parent = "+
           childThreadGroup.getParent().getName());
 System.out.println("childsChildThreadGroup's parent = "+
           childsChildThreadGroup.getParent().getName());
}
}
/*OUTPUT
childThreadGroup's parent = main
childsChildThreadGroup's parent = childThreadGroup
*/
In above program,
The parent of this childThreadGroup is ThreadGroup of the currently running thread i.e. main.
And parent of this childsChildThreadGroup is childThreadGroup.



ThreadGroup important methods >

  • getName()
    • name of ThreadGroup.

  • activeGroupCount()
    • count of active groups in ThreadGroup.

  • activeCount()
    • count of active threads in ThreadGroup.

  • list()
    • list() method has prints ThreadGroups information

  • getMaxPriority()
    • Method returns the maximum priority of ThreadGroup.

  • setMaxPriority(int pri)
    • Sets the maximum priority of ThreadGroup.



Program to demonstrate ThreadGroup - usage of ThreadGroup’s important methods >
public class ThreadGroupTest {
   public static void main(String[] args) {
          ThreadGroup threadGroupMain=Thread.currentThread().getThreadGroup();
          //When program starts JVM creates a ThreadGroup named main
          System.out.println("current ThreadGroup name = "
                       +threadGroupMain.getName());
         
         
          /* all newly created threads
          * become members of the specified ThreadGroup.
          */
          ThreadGroup childThreadGroup1 = new ThreadGroup("childThreadGroup1");         
          Thread thread1 = new Thread(childThreadGroup1,"Thread-1");
      thread1.start();

          ThreadGroup childThreadGroup2 = new ThreadGroup("childThreadGroup2");         
          Thread thread2 = new Thread(childThreadGroup2,"Thread-2");
      thread2.start();

          System.out.println("Currently active groups in main ThreadGroup = "
                       +threadGroupMain.activeGroupCount());
         
          System.out.println("\n--- list() method - Prints information about thread group ---");
          threadGroupMain.list();
   }
}
/*OUTPUT
current ThreadGroup name = main
Currently active groups in main ThreadGroup = 2
--- list() method - Prints information about thread group ---
java.lang.ThreadGroup[name=main,maxpri=10]
   Thread[main,5,main]
   java.lang.ThreadGroup[name=childThreadGroup1,maxpri=10]
   java.lang.ThreadGroup[name=childThreadGroup2,maxpri=10]
*/

OUTPUT ANALYZATION of above program >
Note : I have mentioned output in green text.


current ThreadGroup name = main
JVM creates a ThreadGroup named main.

Currently active groups in main ThreadGroup = 2
childThreadGroup1 and childThreadGroup2 are active ThreadGroups in main ThreadGroup.
Also, childThreadGroup1 and childThreadGroup2 are child of main ThreadGroup.

--- list() method - Prints information about thread group ---
java.lang.ThreadGroup[name=main,maxpri=10]
   Thread[main,5,main] main thread [of main ThreadGroup] is still running.
   java.lang.ThreadGroup[name=childThreadGroup1,maxpri=10]
   java.lang.ThreadGroup[name=childThreadGroup2,maxpri=10]

list() method is used to print ThreadGroups information i.e. its child groups and threads active in each child group. By putting threads on sleep we could see more detailed information about ThreadGroup. For keeping less text over here i have posted program in separate post - Program to demonstrate ThreadGroup - usage of ThreadGroup’s important methods in DETAIL


ThreadGroup and thread priorities >

What is default priority of newly created threadGroup?

ThreadGroup is initialized with default priority of 10.

Program to demonstrate ThreadGroup and thread priorities >
When calling setPriority(newPriority ) method, If newPriority is more than thread’s threadGroup maximum priority, than thread will be set to thread's threadGroup maximum priority.

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ThreadGroupPriorityTest{
  public static void main (String [] args)  {
    
    ThreadGroup threadGroup = new ThreadGroup ("threadGroup");
     System.out.println ("threadGroup's default maximum priority = "
             + threadGroup.getMaxPriority ());
    
     Thread thread1 = new Thread (threadGroup, "Thread-1");
     System.out.println ("thread1's default priority = " + thread1.getPriority ());
      
     threadGroup.setMaxPriority (3);
     System.out.println ("Now, threadGroup's maximum priority = "
             + threadGroup.getMaxPriority ());
     System.out.println ("thread1's priority after changing ThreadGroups's priority = "
             + thread1.getPriority ());
    
     System.out.println("");
    
     Thread thread2 = new Thread (threadGroup, "Thread-2");
     System.out.println ("thread2's default priority = " + thread2.getPriority ());
     thread2.setPriority (10);
     System.out.println ("thread2's priority after setPriority() = " +
                      thread2.getPriority ());
  }
}
/*OUTPUT
threadGroup's default maximum priority = 10
thread1's default priority = 5
Now, threadGroup's maximum priority = 3
thread1's priority after changing ThreadGroups's priority = 5
thread2's default priority = 3
thread2's priority after setPriority() = 3
*/

OUTPUT ANALYZATION of ThreadGroup and thread priorities program >
Note : I have mentioned output in green text.

threadGroup's default maximum priority = 10
threadGroup is created with priority = 10.

thread1's default priority = 5
thread1 is created with default priority of 5.

Now, threadGroup's maximum priority = 3
threadGroup.setMaxPriority (3); method changed threadGroup’s priority to 3.

thread1's priority after changing ThreadGroups's priority = 5
thread1’s priority will remain 5, because ThreadGroup’s priority has been changed after thread1’s creation.


thread2's default priority = 3
threadGroup ’s maximum priority is 3, so thread is set to maximum priority of threadGroup’s priority i.e. 3.

thread2's priority after setPriority() = 3
Even after specifying thread priority by using setPriority() method, new thread’s priority cannot be greater than threadGroup’s priority i.e. 3.



RELATED LINKS>


Thread Pool, Thread local, Busy Spin, ThreadGroup >

Implement Thread pool in java

Implementing ThreadPool using custom LinkedBlockingQueue

ThreadLocal in multithreading in java - methods and usage with program

Busy Spin - What is busy spin? Consumer Producer problem with busy spin and solution to busy spin.

ThreadGroup in java

Demonstrate ThreadGroup's important methods in DETAIL


eEdit
Must read for you :