Skip to content

Advanced Multi threading Concepts

Rohit Agarwal edited this page Jan 12, 2018 · 26 revisions

Thread Group

Based on functionality we can group threads into a single unit which is nothing but a Thread Group. That is Thread Group contains a group of threads.In addition to threads, Thread Group can also contains sub Thread Groups.

The main advantage of maintaining threads in a form of Thread Group is we can perform common operations very easily. Every thread in java belongs to some group. Main thread belongs to Main Group. Every Thread Group in java is a child thread of System Group either directly or indirectly hence System Group acts as root for all Thread Groups in Java. System Group contains several system level threads like Finalizer, Reference Handler, Single Dispatcher, Attach Listener etc.

Example

public class Example1 {

	public static void main(String[] args) {

		System.out.println(Thread.currentThread().getThreadGroup().getName());// Main
		System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());// system

	}

}

ThreadGroup is a java class present in java.lang package and it is direct child of Object.

Constructors

  • ThreadGroup g=new ThreadGroup(String groupName); It creates a new ThreadGroup with the specified group name. The parent of this new group is the ThreadGroup of currently executing thread.
  • ThreadGroup g=new ThreadGroup(ThreadGroup parentGroup, String groupName); It creates a new ThreadGroup with the specified group name. The parent of this new ThreadGroup is specified parent group.

Example

public class Example2 {

	public static void main(String[] args) {

		ThreadGroup g1 = new ThreadGroup("First Group");
		System.out.println(g1.getParent().getName());// Main
		ThreadGroup g2 = new ThreadGroup(g1, "Second Group");
		System.out.println(g2.getParent().getName());// First Group

	}

}

Important methods

  1. String getName(); - It returns name of the ThreadGroup.
  2. int getMaxPriority(); - It returns max priority of ThreadGroup.
  3. void setMaxPriority(int p); - It set max priority of ThreadGroup. The default max priority is 10. Threads in the ThreadGroup that already has higher priority won't be affected but for newly added threads this max priority is applicable.
  4. ThreadGroup getParent(); - It returns the parent group of current ThreadGroup.
  5. void list(); - It prints information about ThreadGroup on the console.
  6. int activeCount(); - It returns number of active threads in current ThreadGroup.
  7. int activeGroupCount(); - It returns number of active groups present in the current ThreadGroup.
  8. int enumerate(Thread[] t); - It copy all the active threads of current ThreadGroup into provided thread array. In this case sub ThreadGroup threads will also considered.
  9. int enumerate(ThreadGroup[] g); - It copy all active sub ThreadGroups into ThreadGroup array.
  10. boolean isDaemon(); - It checks whether ThreadGroup is daemon or not.

Clone this wiki locally