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.
  11. void setDaemon(boolean b);
  12. void interrupt(); - To interrupt all waiting or sleeping threads present in the ThreadGroup.
  13. void destroy(); - To destroy ThreadGroup and its sub ThreadGroup.

Example

public class Example3 {

	/*
	 * Threads in the ThreadGroup that already have higher priority won't be
	 * affected but for newly added threads this max priority is applicable.
	 * 
	 */
	public static void main(String[] args) {

		ThreadGroup g1 = new ThreadGroup("Thread Group");
		Thread t1 = new Thread(g1, "Thread1");
		Thread t2 = new Thread(g1, "Thread2");
		g1.setMaxPriority(3);
		Thread t3 = new Thread(g1, "Thread3");
		System.out.println(t1.getPriority());// 5
		System.out.println(t2.getPriority());// 5
		System.out.println(t3.getPriority());// 3

	}

}
public class Example4 {


	public static void main(String[] args) throws InterruptedException {
		
		ThreadGroup pg=new ThreadGroup("Parent Group");
		ThreadGroup cg=new ThreadGroup(pg,"Child Group");
		MyThread t1=new MyThread(pg,"Child Thread1");
		MyThread t2=new MyThread(pg, "Child Thread2");
		t1.start();
		t2.start();
		System.out.println(pg.activeCount());//2
		System.out.println(pg.activeGroupCount());//1
		pg.list();
		Thread.sleep(10000);
		System.out.println(pg.activeCount());//0
		System.out.println(pg.activeGroupCount());//1
		pg.list();
	}

}

class MyThread extends Thread{
	
	public MyThread(ThreadGroup g,String name) {
		super(g,name);
	}
	
	public void run(){
		System.out.println("Child Thread");
		try{
			Thread.sleep(5000);
		}catch(InterruptedException exception){
			
		}
	}
	
}
//Write a program to display all active thread names belong to system group.
public class Example5 {

	public static void main(String[] args) {

		ThreadGroup systemGroup=Thread.currentThread().getThreadGroup().getParent();
		Thread[] t=new Thread[systemGroup.activeCount()];
		systemGroup.enumerate(t);
		for(Thread t1:t){
			System.out.println(t1.getName()+" is Daemon Thread -> "+t1.isDaemon());
		}
		
	}

}

java.util.concurrent package

The problem with traditional synchronized keyword.

  1. We are not having any flexibility to try for a lock without waiting.
  2. There is no way to specify maximum waiting time for a thread to get lock so that thread will wait until getting the lock which may creates performance problems which may cause deadlock.
  3. If a thread releases a lock then which waiting thread will get that lock we are not having any control on this.
  4. There is no API to list out all waiting threads for a lock.
  5. Synchronized keyword compulsory we have to used either at method level or within the method and it is not possible to use across multiple methods.
Clone this wiki locally