Demonstrate ThreadGroup's important methods in DETAIL in java


Contents of page :
  • Program to demonstrate ThreadGroup - usage of ThreadGroup’s important methods in DETAIL >
  • OUTPUT ANALYZATION of above program >


In my previous post, we ThreadGroup. in this post i will be writing programs to demonstrate ThreadGroup’s important methods.
In below program we will be putting threads on sleep to see detailed information about ThreadGroup.



Program to demonstrate ThreadGroup - usage of ThreadGroup’s important methods in DETAIL >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
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());
         
          MyRunnable myRunnable=new MyRunnable();
          ThreadGroup childThreadGroup1 = new ThreadGroup("childThreadGroup1");         
          Thread thread1 = new Thread(childThreadGroup1,myRunnable,"Thread-1");
          thread1.start();
         
          ThreadGroup childThreadGroup2 = new ThreadGroup("childThreadGroup2");         
          Thread thread2 = new Thread(childThreadGroup2,myRunnable,"Thread-2");
          thread2.start();
         
          System.out.println("Currently active groups in main ThreadGroup = "
                       +threadGroupMain.activeGroupCount());
          System.out.println("Currently active threads in main ThreadGroup = "
                       +threadGroupMain.activeCount());
         
           System.out.println("\nchildThreadGroup1's parent ThreadGroup = "+
                        childThreadGroup1.getParent().getName());
           System.out.println("Thread-1's ThreadGroup = "
                              +thread1.getThreadGroup().getName());
         
           System.out.println("childThreadGroup2's parent ThreadGroup = "+
                        childThreadGroup2.getParent().getName());
           System.out.println("Thread-2's ThreadGroup = "
                                     +thread2.getThreadGroup().getName());
                
         
          System.out.println("\n--- list() method - Prints information about thread group ---");
          threadGroupMain.list();
   }
}
class MyRunnable implements Runnable{
   @Override
   public void run() {
          try {
                 Thread.sleep(1000);
          } catch (InterruptedException e) {
                 e.printStackTrace();
          }
   }
  
}
/*OUTPUT
current ThreadGroup name = main
Currently active groups in main ThreadGroup = 2
Currently active threads in main ThreadGroup = 3
childThreadGroup1's parent ThreadGroup = main
Thread-1's ThreadGroup = childThreadGroup1
childThreadGroup2's parentThreadGroup = main
Thread-2's ThreadGroup = childThreadGroup2
--- 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]
    Thread[Thread-1,5,childThreadGroup1]
   java.lang.ThreadGroup[name=childThreadGroup2,maxpri=10]
    Thread[Thread-2,5,childThreadGroup2]
*/




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.

Currently active threads in main ThreadGroup = 3
main thread, thread-1 and thread-2 are 3 active threads.
childThreadGroup1's parent ThreadGroup = main
Thread-1's ThreadGroup = childThreadGroup1
childThreadGroup2's parent ThreadGroup = main
Thread-2's ThreadGroup = childThreadGroup2
parent of this childThreadGroup1 and childThreadGroup2 is main
Thread-1 belongs to childThreadGroup1 ThreadGroup, and
Thread-2 belongs to childThreadGroup2 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]
    Thread[Thread-1,5,childThreadGroup1] thread-1 [of main childThreadGroup1] is still running.
   java.lang.ThreadGroup[name=childThreadGroup2,maxpri=10]
    Thread[Thread-2,5,childThreadGroup2] thread-2 [of main childThreadGroup2] is still running.


list() method has printed ThreadGroups information i.e. its child groups and threads active in each child group. Because of sleep() method, threads were still running. (Sleep was just used to describe functionality of method in detail)



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


No comments:

Post a Comment