Java Thread group
ThreadGroup is a class which is used for creating group of threads. This group of threads are in the form of a tree structure, in which the initial thread is the parent thread. A thread can have all the information of the other threads in the groups but can have the information of the threads of the other groups. It is very useful in the case where we want to suspend and resume numbers of threads. This thread group is implemented by java.lang.ThreadGroup class.
There are two types of Constructors in the Thread group they are as follow:
1. public ThreadGroup(String name)
2. public ThreadGroup(ThreadGroup parent, String name)
Following are the methods present in Thread group
1. checkAccess()
In Java, checkAccess()
method is of ThreadGroup class. It is used to check whether the running thread has permission for modification or not in the ThreadGroup.
Syntax
public final void checkAccess()
Example:
class ThreadDemo1_1 extends Thread
{
ThreadDemo1_1(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (int i = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName());
}
}
System.out.println(Thread.currentThread().getName());
}
}
public class ThreadDemo1
{
public static void main(String arg[]) throws InterruptedException, SecurityException
{
ThreadGroup obj1 = new ThreadGroup("Parent thread =====> ");
ThreadGroup obj2 = new ThreadGroup(obj1, "child thread =====> ");
ThreadDemo1_1 t1 = new ThreadDemo1_1("*******Thread-1*******", obj1);
t1.start();
ThreadDemo1_1 t2 = new ThreadDemo1_1("*******Thread-2*******", obj1);
t2.start();
obj1.checkAccess();
System.out.println(obj1.getName() + " has access");
obj2.checkAccess();
System.out.println(obj2.getName() + " has access");
}
}
2. activeCount()
In Java, activeCount()
method is of ThreadGroup class. It is used to count the active threads in a group which are currently running.
public static int activeCount()
Example:
class Demo2 extends Thread
{
Demo2 (String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (inti = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
Syste m.out.println(Thread.currentThread().getName());
}
}
System.out.println(Thread.currentThread().getName());
}
}
public class ThreadDemo2
{
public static void main(String arg[])
{
ThreadGroup o1 = new ThreadGroup("parent thread group");
Demo2 obj1 = new Demo2 ("Thread 1 =====> ", o1);
Demo2 obj2 = new Demo2 ("Thread 2 =====> ", o1);
Demo2 obj3 = new Demo2 ("Thread 3 =====> ", o1);
Demo2 obj4 = new Demo2 ("Thread 4 =====> ", o1);
Demo2 obj5 = new Demo2 ("Thread 5 =====> ", o1);
Demo2 obj6 = new Demo2 ("Thread 6 =====> ", o1);
obj1.start();
obj2.start();
obj3.start();
obj4.start();
obj5.start();
obj6.start();
System.out.println("Total number of active thread =====> "+ o1.activeCount());
}
}
3. activeGroupCount()
In Java, activeGroupCount()
method is of ThreadGroup class. It is used to count the active groups of threads which are currently running.
Syntax public int activeGroupCount().
public int activeGroupCount().
Example:
class Demo2 extends Thread
{
Demo2 (String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (inti = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName());
}
}
System.out.println(Thread.currentThread().getName()+" =====> completed executing");
}
}
public class ThreadDemo2
{
public static void main(String arg[])
{
ThreadGroup o1 = new ThreadGroup("parent thread group");
ThreadGroup o2 = new ThreadGroup(o1,"Child thread group");
ThreadGroup o3 = new ThreadGroup(o1,"parent thread group");
Demo2 obj1 = new Demo2("*****Thread 1*****",o1);
System.out.println(obj1.getName() + " =====> starts");
obj1.start();
System.out.println("Total number of active thread =====> "+ o1.activeGroupCount());
}
}
4. destroy()
In Java, the destroy()
method is of ThreadGroup. It used to destroy a thread group. For destroying any thread group it is important that all the threads in that group should be stopped.
syntax
public void destroy()
Example:
class Demo2 extends Thread
{
Demo2 (String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (inti = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName());
}
}
System.out.println(Thread.currentThread().getName()+" =====> completed executing");
}
}
public class ThreadDemo2
{
public static void main(String arg[]) throws InterruptedException, SecurityException
{
ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");
ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****");
Demo2 obj1 = new Demo2("*****Thread 1*****",o1);
obj1.start();
Demo2 obj2 = new Demo2("*****Thread 2*****",o1);
obj2.start();
obj1.join();
obj2.join();
o2.destroy();
System.out.println(o2.getName()+" ====> Destroyed");
o1.destroy();
System.out.println(o1.getName()+" ====> Destroyed");
}
}
5. enumerate(Thread[] list)
In Java, enumerate()
method is of ThreadGroup class. It is used for copying active threads into a specified array.
public int enumerate(Thread[] array)
Example:
class Demo2 extends Thread
{
Demo2 (String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (inti = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName());
}
}
System.out.println(Thread.currentThread().getName()+" =====> completed executing");
}
}
public class ThreadDemo2
{
public static void main(String arg[])
{
ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");
ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****");
Demo2 obj1 = new Demo2("*****Thread 1*****",o1);
System.out.println("Thread 1 Starts");
obj1.start();
Demo2 obj2 = new Demo2("*****Thread 2*****",o1);
System.out.println("Thread 2 Starts");
obj2.start();
Thread[] tarray = new Thread[o1.activeCount()];
int count1 = o1.enumerate(tarray);
for (inti = 0; i< count1; i++)
System.out.println(tarray[i].getName() + " =====> Found");
}
}
6. getMaxPriority()
In Java, getMaxPriority() method is of ThreadGroup class. It is used for check the maximum priority of the thread group.
Syntax
public final int getMaxPriority()
Example:
class Demo2 extends Thread
{
Demo2 (String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (inti = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName());
}
}
System.out.println(Thread.currentThread().getName()+" =====> completed executing");
}
}
public class ThreadDemo2
{
public static void main(String arg[])
{
ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");
System.out.println("Maximum priority of Parent Thread: " + o1.getMaxPriority());
ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****");
System.out.println("Maximum priority of Child Thread: " + o2.getMaxPriority());
Demo2 obj1 = new Demo2("*****Thread 1*****",o1);
System.out.println("Thread 1 Starts");
obj1.start();
Demo2 obj2 = new Demo2("*****Thread 2*****",o1);
System.out.println("Thread 2 Starts");
obj2.start();
}
}
7. getName()
In Java, getName()
method is of ThreadGroup class. It is used for getting the name of the current thread group.
public final String getName()
Example:
class Demo3 extends Thread
{
Demo3(String a, ThreadGroup b)
{
super(b, a);
start();
}
public void run()
{
System.out.println(Thread.currentThread().getName());
}
}
public class ThreadDemo3
{
public static void main(String arg[]) throws InterruptedException,
SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
ThreadGroup o2 = new ThreadGroup(o1, "*****Child thread*****");
Demo3 obj1 = new Demo3("Thread-1", o1);
System.out.println("Name of First threadGroup : " +obj1.getThreadGroup().getName());
Demo3 obj2 = new Demo3("Thread-2", o2);
System.out.println("Name of Second threadGroup: " +obj2.getThreadGroup().getName());
}
}
8. getParent()
In Java, getParent()
method is of ThreadGroup class. It is used to get the parent thread from the thread group.
public final ThreadGroup getParent()
Example:
class Demo3 extends Thread
{
Demo3(String a, ThreadGroup b)
{
super(b, a);
start();
}
public void run()
{
System.out.println(Thread.currentThread().getName());
}
}
public class ThreadDemo3
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
ThreadGroup o2 = new ThreadGroup(o1, "*****Child thread*****");
Demo3 obj1 = new Demo3("Thread-1", o1);
System.out.println("Thread one starting");
obj1.start();
Demo3 obj2 = new Demo3("Thread-2", o2);
System.out.println("Thread second starting");
obj2.start();
System.out.println("Parent Thread Group for " + o1.getName() + " is " + o1.getParent().getName());
System.out.println("Parent Thread Group for " + o2.getName() + " is " + o2.getParent().getName());
}
}
9. interrupt()
In Java, interrupt()
method is of ThreadGroup class. It is used to interrupt all the threads of the thread group.
Syntax
public final void interrupt()
Example:
class Demo2 extends Thread
{
Demo2 (String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (inti = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
System.out.println(Thread.currentThread().getName()+ " =====> interrupted");
}
}
System.out.println(Thread.currentThread().getName()+" =====> completed executing");
}
}
public class ThreadDemo2
{
public static void main(String arg[]) throws InterruptedException, SecurityException
{
ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");
ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****");
Demo2 obj1 = new Demo2("*****Thread 1*****",o1);
System.out.println(obj1.getName()+"Thread 1 Starts");
obj1.start();
o1.interrupt();
Demo2 obj2 = new Demo2("*****Thread 2*****",o1);
System.out.println(obj2.getName()+"Thread 2 Starts");
obj2.start();
}
}
10. isDaemon()
In Java, isDaemon()
method is of ThreadGroup class. It is used to check whether a thread is Daemon thread or not.
Syntax
public final boolean isDaemon()
Example:
class Demo4 extends Thread
{
Demo4(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for(inti = 0;i < 10;i++)
{
i++;
}
System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");
}
}
public class ThreadDemo4
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
Demo4 obj1 = new Demo4 ("*****Thread-1*****", o1);
obj1.start();
System.out.println("Is " + o1.getName() + " a daemon threadGroup? " + o1.isDaemon());
}
}
11. setDaemon(Boolean daemon)
In Java, setDaemon()
method is of ThreadGroup class. It is used to make a thread as a daemon thread.
Syntax:
public final void setDaemon(boolean daemon)
Example:
class Demo4 extends Thread
{
Demo4(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for(int i = 0;i < 10;i++)
{
i++;
}
System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");
}
}
public class ThreadDemo4
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
o1.setDaemon(true);
ThreadGroup o2 = new ThreadGroup("*****Child thread*****");
o2.setDaemon(true);
Demo4 obj1 = new Demo4 ("*****Thread-1*****", o1);
obj1.start();
Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2);
obj2.start();
System.out.println("Is " + o1.getName() + " a daemon threadGroup? " + o1.isDaemon());
System.out.println("Is " + o2.getName() + " a daemon threadGroup? " + o2.isDaemon());
}
}
12. isDestoryed()
In Java, isDestroyed()
method is of ThreadGroup class. It is used to check whether the thread group is destroyed or not.
Syntax
public boolean isDestroyed()
Example:
class Demo4 extends Thread
{
Demo4(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for(int i = 0;i < 10;i++)
{
i++;
}
System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");
}
}
public class ThreadDemo4
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
ThreadGroup o2 = new ThreadGroup("*****Child thread*****");
Demo4 obj1 = new Demo4 ("*****Thread-1*****", o1);
System.out.println("Starting Thread 1");
obj1.start();
Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2);
System.out.println("Starting Thread 2");
obj2.start();
if(o1.isDestroyed()==true)
System.out.println("The Group is destroyed");
else
System.out.println("The Group is not destroyed");
}
}
13. list()
In Java, the list()
method is of ThreadGroup class. It is used to getting all the information about a thread group. It is very useful at the time of debugging.
Syntax
public void list()
Example:
class Demo4 extends Thread
{
Demo4(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for(int i = 0;i < 10;i++)
{
i++;
}
System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");
}
}
public class ThreadDemo4
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
ThreadGroup o2 = new ThreadGroup("*****Child thread*****");
Demo4 obj1 = new Demo4("*****Thread-1*****", o1);
System.out.println(obj1.getName()+"Starting Thread 1");
obj1.start();
Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2);
System.out.println(obj2.getName()+"Starting Thread 2");
obj2.start();
System.out.println("List of parent Thread Group: " + o1.getName() + ":");
o1.list();
}
}
14. ParentOf(ThreadGroup g)
In Java, ParentOf()
method is of ThreadGroup class. It is used to check whether the current running thread is the Parent thread of which ThreadGroup.
Syntax
public final boolean parentOf(ThreadGroup g)
Example:
class Demo4 extends Thread
{
Demo4(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for(int i = 0;i < 10;i++)
{
i++;
}
System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");
}
}
public class ThreadDemo4
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
ThreadGroup o2 = new ThreadGroup("*****Child thread*****");
Demo4 obj1 = new Demo4("*****Thread-1*****", o1);
System.out.println(obj1.getName()+"Starting Thread 1");
obj1.start();
Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2);
System.out.println(obj2.getName()+"Starting Thread 2");
obj2.start();
boolean isParent = o2.parentOf(o1);
System.out.println(o2.getName() + " is the parent of " + o1.getName() +": "+ isParent);
isParent = o1.parentOf(o2);
System.out.println(o1.getName() + " is the parent of " + o2.getName() +": "+ isParent);
}
}
15. suspend()
In Java, suspend()
method is of ThreadGroup class. It is used to suspend all threads of the thread group.
Syntax
public final void suspend()
Example:
class Demo4 extends Thread
{
Demo4(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for(int i = 0;i < 10;i++)
{
i++;
}
System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");
}
}
public class ThreadDemo4
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
ThreadGroup o2 = new ThreadGroup("*****Child thread*****");
Demo4 obj1 = new Demo4("*****Thread-1*****", o1);
System.out.println(obj1.getName()+"Starting Thread 1");
obj1.start();
Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2);
System.out.println(obj2.getName()+"Starting Thread 2");
obj2.start();
o1.suspend();
}
}
16. resume()
In Java, resume()
method is of ThreadGroup class.It is used to resume all threads of the thread group which were suspended.
Syntax
public final void resume()
Example:
class Demo4 extends Thread
{
Demo4(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for(int i = 0;i < 10;i++)
{
i++;
}
System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");
}
}
public class ThreadDemo4
{
public static void main(String arg[]) throws InterruptedException, SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");
ThreadGroup o2 = new ThreadGroup("*****Child thread*****");
Demo4 obj1 = new Demo4("*****Thread-1*****", o1);
System.out.println(obj1.getName()+"Starting Thread 1");
obj1.start();
o1.suspend();
Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2);
System.out.println(obj2.getName()+"Starting Thread 2");
obj2.start();
o1.resume();
}
}
17. setMaxPriority(intpri)
In Java, setMaxPriority()
method is of ThreadGroup class. It is used to set the maximum priority of the thread group.
Syntax
public final void setMaxPriority(int pri)
Example:
class Demo5 extends Thread
{
Demo5(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (int i = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex) {
System.out.println("Thread " + Thread.currentThread().getName() + " =====> Interrupted"); }
}
System.out.println(Thread.currentThread().getName() + " [Priority = " +
Thread.currentThread().getPriority() + "]");
System.out.println(Thread.currentThread().getName()+" =====> Execution finish");
}
}
public class ThreadDemo5
{
public static void main(String arg[]) throws InterruptedException,
SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent threadGroup*****");
ThreadGroup o2 = new ThreadGroup(o1, "*****Child threadGroup*****");
o1.setMaxPriority(Thread.MAX_PRIORITY-3);
o2.setMaxPriority(Thread.MIN_PRIORITY);
Demo5 obj1 = new Demo5("*****Thread-1*****", o1);
obj1.setPriority(Thread.MAX_PRIORITY);
System.out.println(obj1.getName() + " ====> starts");
obj1.start();
Demo5 obj2 = new Demo5("*****Thread-2*****", o2);
obj2.setPriority(Thread.MAX_PRIORITY);
System.out.println(obj2.getName() + " ====> starts");
obj2.start();
}
}
18. stop()
In Java, stop()
method is of ThreadGroup class. It is used to stop threads of the thread group.
Syntax
public final void stop()
Example:
class Demo5 extends Thread
{
Demo5(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (int i = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex) {
System.out.println("Thread " + Thread.currentThread().getName() + " =====> Interrupted"); }
}
System.out.println(Thread.currentThread().getName() + " [Priority = " +
Thread.currentThread().getPriority() + "]");
System.out.println(Thread.currentThread().getName()+" =====> Execution finish");
}
}
public class ThreadDemo5
{
public static void main(String arg[]) throws InterruptedException,
SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent threadGroup*****");
ThreadGroup o2 = new ThreadGroup(o1, "*****Child threadGroup*****");
o1.setMaxPriority(Thread.MAX_PRIORITY-3);
o2.setMaxPriority(Thread.MIN_PRIORITY);
Demo5 obj1 = new Demo5("*****Thread-1*****", o1);
System.out.println("Thread one starting");
obj1.start();
Demo5 obj2 = new Demo5("*****Thread-2*****", o2);
System.out.println("Thread second starting");
obj2.start();
o1.stop();
}
}
19. toString()
In Java, toString()
method is of ThreadGroup class. It is used to get the string representation of a thread group.
Syntax
public String toString()
Example:
class Demo5 extends Thread
{
Demo5(String a, ThreadGroup b)
{
super(b, a);
}
public void run()
{
for (int i = 0; i< 10; i++)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException ex) {
System.out.println("Thread " + Thread.currentThread().getName() + " =====> Interrupted"); }
}
System.out.println(Thread.currentThread().getName() + " [Priority = " +
Thread.currentThread().getPriority() + "]");
System.out.println(Thread.currentThread().getName()+" =====> Execution finish");
}
}
public class ThreadDemo5
{
public static void main(String arg[]) throws InterruptedException,
SecurityException, Exception
{
ThreadGroup o1 = new ThreadGroup("*****Parent threadGroup*****");
ThreadGroup o2 = new ThreadGroup(o1, "*****Child threadGroup*****");
Demo5 obj1 = new Demo5("*****Thread-1*****", o1);
System.out.println(obj1.getName() + " ====> starts");
obj1.start();
Demo5 obj2 = new Demo5("*****Thread-2*****", o2);
System.out.println(obj2.getName() + " ====> starts");
obj2.start();
System.out.println("String equivalent: " + o1.toString());
System.out.println("String equivalent: " + o2.toString());
}
}