Blog

Multithreading Enhancement

Published on

ThreadGroup

Based on functionality we can group thread in a single unit which is nothing but ThreadGroup. 

ThreadGroup contains a group of  thread. In addition to threads, ThreadGroup can also contain sub thread group.

The main advantage of maintaining threads in the form of threadgroup is we can perform common operations very easily.

Every thread in java belongs to some group. Main thread belongs to main group. Every ThreadGroup in java is the child group of system group either directly or indirectly. System group access root for all thread groups in java. System group containes several system level threads like below:

finalizer, reference handler, signal dispatcher, attach listner etc.

ThreadGroup is a java class present in java.lang package and it is the direct child xlass of object.

Constructors

ThreadGroup g=new ThreadGroup(String groupName);

ThreadGroup g=new ThreadGroup(ThreadGroup tg,String groupName);

The parent of this  new threadgroup is specified parent threadgroup.

Threads in the threadgroup that have already higher priority will not be affected & for newly added thread this max priority is applicable . example is given below

 

				
					public class ThreadGroupDemo {

	public static void main(String[] args) {

		ThreadGroup g1 = new ThreadGroup("tg");
		Thread t1 = new Thread(g1, "t1");

		g1.setMaxPriority(3);
		System.out.println(g1.getMaxPriority());
		System.out.println(t1.getPriority());
		Thread t2 = new Thread(g1, "t2");
		System.out.println(t2.getPriority());

	}
}

				
			

Popular Posts

Copyright © 2024 javadsa.com