ThreadGroup:
Based on the functionality we can group thread into a single unit which is nothing but a thread group i.e a thread group contains a group of threads.
In addition to that, it also contains a sub-thread group
The main advantage of maintaining threads in the form of the thread group is we can perform common operations very easily.
Note:-1. Every thread in java belong to some group, main thread belong to the main group
2. Every thread group in java is the child group of the system group either directly or indirectly, hence a system group act as root/parent for all thread groups.
System group contains several system levels thread:
1. finalizer
2. Reference handler
3. Attach Listener
4. Signal Dispatcher, etc.
Program to illustrate that every thread in java belongs to some group and directly or indirectly child of system group:
import java.lang.*;
class ThreadGroupDemo1{
public static void main(String args[])
{
System.out.println(Thread.currentThread().getThreadGroup().getName());
System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
}
}
Constructors:
- ThreadGroup g=new ThreadGroup(String name)- create a new thread group with the specified group name. The parent of these new groups is the thread group of currently executing threads.
- ThreadGroup g=new ThreadGroup(ThreadGroup g, String name)-create a new thread group with the specified group name the parent of this new thread group is specified parent group.
Methods:
- String getName()- return the name of the thread group.
- int getMaxPriority()- return maximum priority of thread group.
- void setMaxPriority()- To set the maximum priority of the thread group. The default maximum priority is 10.
- void list()- it will print information about the thread group to the console.
- int activeCount()- return the number of active thread present in a thread group.
Programs:
import java.lang.*;
import java.util.*;
class ThreadGroupDemo2 extends Thread
{
public static void main(String args[])
{
ThreadGroup g=new ThreadGroup("tg");
Thread t1=new Thread(g,"Thread 1");
Thread t2=new Thread(g,"Thread 2");
g.setMaxPriority(3);//setting priority for ThreadGroup
Thread t3=new Thread(g,"Thread 3");
System.out.println("Default Priority t1: "+t1.getPriority());
System.out.println("Default Priority t2: "+t2.getPriority());
System.out.println("Default Priority t3: "+t3.getPriority());
}
}
class MyThread extends Thread
{
MyThread(ThreadGroup g,String name)
{
super(g,name);
}
public void run()
{
System.out.println("child Thread");
try{Thread.sleep(5000);}catch(Exception e){}
}
}
class ThreadGroupDemo3
{
public static void main(String args[])throws Exception
{
ThreadGroup p=new ThreadGroup("Parent");
ThreadGroup g=new ThreadGroup(p,"child group");
MyThread t1=new MyThread(g,"childThread1");
MyThread t2=new MyThread(g,"childThread2");
t1.start();
t2.start();
System.out.println("Thread count: "+p.activeCount()); //2
System.out.println("Group count: "+p.activeGroupCount());//1
System.out.println("child group: "+g.activeGroupCount());//0
p.list();
Thread.sleep(10000);
System.out.println("Thread count: "+p.activeCount());
System.out.println("Group count: "+p.activeGroupCount());
System.out.println("child group: "+g.activeGroupCount());
}
}
import java.lang.*;
class ThreadGroupDemo4 extends Thread
{
public static void main(String args[])
{
ThreadGroup system=Thread.currentThread().getThreadGroup().getParent();
Thread[] t=new Thread[system.activeCount()];
system.enumerate(t);
for(Thread t1:t)
System.out.println(t1.getName()+"...."+t1.isDaemon());
}
}

Comments
Post a Comment