Class ThreadGroup in Java

digitization, transformation, binary

Class ThreadGroup in Java:

A ThreadGroup is a set or collection of threads. We can include different threads and ThreadGroups in a single ThreadGroup. so a ThreadGroup can contain several threads and ThreadGroups as its member elements. In that case, it will form a tree structure popularly known as the thread tree.

In the thread tree, every thread and ThreadGroup will have a parent except the initial ThreadGroup.A thread can access it’s own ThreadGroup but not other ThreadGroup. We can perform operations on the group as a whole instead of individual threads. ThreadGroups are mainly applicable to system-level applications.

Ads code goes here

In java, there is a default ThreadGroup called SystemThreadGroup. When a java application is started, the java runtime creates the main thread group as a member of SystemThreadGroup. The main thread is created in the main thread group to run the main() method of the application.

Every thread instance is a member of exactly one thread group. By default, all new threads and thread groups created when objects are instantiated will become the member of the main thread group. All threads in an application will form a tree with the SystemThreadGroup as root.

The structure of the ThreadGroup class is as follows:

public class java.lang.ThreadGroup extends java.lang.Object{
//constructors
public ThreadGroup(String name)//constructs a new ThreadGroup whose parent is the ThreadGroup of the currently running thread.
public ThreadGroup(ThreadGroup parent,String name);//constructs a ThreadGroup whose parent is provided in the argument and it can throw NullPointerException if the ThreadGroup argument is null. It can also throw SecurityException if the current thread can not create a thread in the specified ThreadGroup.
//Methods:
public int activeCount();
public int activeGroupCount();
public final void checkAccess();
public final void destroy();
public int enumerate(Thread list[]);
public int enumerate(Thread list[],boolean recurse);
public int enumerate(ThreadGroup list[]);
public int enumerate(ThreadGroup list[],boolean recurse);
public final void resume();
public final void setDeamon(boolean deamon);
public final void setMaxPriority(int priority);
public final void stop();
public final void suspend();
public final int getMaxPriority();
public final String getName();
public final ThreadGroup getParent();
public final boolean isDeamon();
public boolean allowThreadSuspension(boolean b);
public synchronized boolean isDestroyed();
public void list();
public final boolean parentOf(ThreadGroup group);
public void UncaughtException(Thread t,Throwable e);
public String toString();
} 

The details of the ThreadGroup is given as:

public ThreadGroup(String name);

public ThreadGroup(String name) constructor creates a ThreadGroup object that has the specified name and the same parent ThreadGroup as the current thread.
Parameter
name -The name of this ThreadGroup object.

public ThreadGroup(ThreadGroup parent, String name;

public ThreadGroup(ThreadGroup parent, String name) constructor creates a ThreadGroup object with the specified name and parent ThreadGroup object.
Parameter
parent -The ThreadGroup object that this ThreadGroup object is to be added to.
name – The name of this ThreadGroup object.

public int activeCount();

public int activeCount() method returns an approximation of the number of threads that belong to this ThreadGroup object and any child ThreadGroup objects. The count is approximate because a thread can die after it is counted, but before the complete count is returned. Also, after a child, ThreadGroup is counted but before the total count is returned, additional Thread and ThreadGroup objects can be added to a child ThreadGroup.

READ  How to stop a Test Process If that Is Hanged or Went To Infinite loop

This method returns an approximation of the current number of threads in this ThreadGroup object and any child ThreadGroup objects.

public int activeGroupCount();

public int activeGroupCount() method returns an approximation of the number of child ThreadGroup objects that belong to this ThreadGroup object. The count is approximate because after a child ThreadGroup is counted but before the total count is returned, additional ThreadGroup objects can be added to a child ThreadGroup.

This method returns an approximation of the current number of child ThreadGroup objects in this ThreadGroup object.

public boolean allowThreadSuspension(boolean b); 

public boolean allowThreadSuspension(boolean b); method specifies whether or not the Java virtual machine is allowed to suspend threads due to low memory.

This method returns the boolean value true.

Parameter
b – A boolean value that specifies whether or not the run-time system is allowed to suspend
threads due to low memory.

public final void checkAccess();

public final void checkAccess() method determines if the currently running thread has permission to modify this ThreadGroup object.

public final void destroy();

public final void destroy() method destroys this ThreadGroup object and any child ThreadGroup objects. The ThreadGroup must not contain any Thread objects. This method also removes the ThreadGroup object from its parent ThreadGroup object.

public int enumerate(Thread list[]);

public int enumerate(Thread list[]) method stores a reference in the array for each of the Thread objects that belongs to this ThreadGroup or any of its child ThreadGroup objects.
If the array is not big enough to contain references to all the Thread objects, only as many
references, as will fit, are put into the array.

No indication is given that some Thread objects were left out, so it is a good idea to call activeCount() before calling this method, to get an idea of how large to make the array.

This method returns the number of Thread objects stored in the array.

Parameter
list – A reference to an array of Thread objects.

public int enumerate(Thread list[], boolean recurse);

public int enumerate(Thread list[], boolean recurse) method stores a reference in the array for each of the Thread objects that belong to this ThreadGroup object. If recurse is true, the method also stores a reference for each of the Thread objects that belongs to a child ThreadGroup object of this ThreadGroup.

If the array is not big enough to contain references to all the Thread objects, only as many
references as will fit are put into the array. No indication is given that some Thread objects were left out, so it is a good idea to call activeCount() before calling this method, to get an idea of how large to make the array.

This method returns the number of Thread objects stored in the array.
Parameter
list – A reference to an array of Thread objects.
recurse – A boolean value that specifies whether or not to include Thread objects that belong to child ThreadGroup objects of this ThreadGroup object.

public int enumerate(ThreadGroup list[]);

public int enumerate(ThreadGroup list[]) method stores a reference in the array for each ThreadGroup object that belongs to this ThreadGroup or any of its child ThreadGroup objects.

READ  Java Documentation-JavaDoc Concept Simplified in Java

If the array is not big enough to contain references to all the ThreadGroup objects, only as many references as will fit are put into the array. No indication is given that some ThreadGroup objects were left out, so it is a good idea to call activeGroupCount() before calling this method, to get an idea of how large to make the array.

This method returns the number of ThreadGroup objects stored in the array.
Parameter
list – A reference to an array of ThreadGroup objects.

public int enumerate(ThreadGroup list[], boolean recurse);

public int enumerate(ThreadGroup list[], boolean recurse) method stores a reference in the array for each of the ThreadGroup objects that belong to this ThreadGroup object. If recurse is true, the method also stores a reference for each of the ThreadGroup objects that belongs to a child ThreadGroup object of this ThreadGroup.

If the array is not big enough to contain references to all the ThreadGroup objects, only as many references as will fit are put into the array. No indication is given that some ThreadGroup objects were left out, so it is a good idea to call activeGroupCount() before calling this method, to get an idea of how large to make the array.

This method returns the number of ThreadGroup objects stored in the array.

Parameter
list – A reference to an array of ThreadGroup objects.
recurse – A boolean value that specifies whether or not to include ThreadGroup objects that belong
to child ThreadGroup objects of this ThreadGroup object.

public final int getMaxPriority();

public final int getMaxPriority() method returns the maximum priority that can be assigned to Thread objects that belong to this ThreadGroup object.

It is possible for a ThreadGroup to contain Thread objects that have higher priorities than this
maximum, if they were given that higher priority before the maximum was set to a lower value.

This method returns the maximum priority that can be assigned to Thread objects that belong to this ThreadGroup object.

public final String getName();

public final String getName() method returns the name of this ThreadGroup object.
This method returns the name of this ThreadGroup object.

public final ThreadGroup getParent();

public final ThreadGroup getParent() method returns the parent ThreadGroup object of this ThreadGroup object. If this ThreadGroup is at the root of the thread group hierarchy and has no parent, the method returns null.

This method returns the parent ThreadGroup object of this ThreadGroup, or null if this ThreadGroup is the root of the thread group hierarchy.

public final boolean isDaemon();

public final boolean isDaemon() method determines whether or not this ThreadGroup is a daemon thread group, based on the value of daemon attribute of this ThreadGroup object. A daemon thread group is destroyed when the last Thread in it is stopped, or the last ThreadGroup in it is destroyed.

This method returns true if this ThreadGroup is a daemon thread group; otherwise false.

public synchronized boolean isDestroyed(); 

public synchronized boolean isDestroyed(); method determines whether or not this ThreadGroup has been destroyed.

READ  Class InvocationTargetException in java

This method returns true if this ThreadGroup has been destroyed; otherwise false.

public void list();

public void list() method outputs a listing of the contents of this ThreadGroup object to System.out.

public final boolean parentOf(ThreadGroup g);

public final boolean parentOf(ThreadGroup g) method determines if this ThreadGroup object is the same as the specified ThreadGroup or one of its ancestors in the thread-group hierarchy.

This method returns true if this ThreadGroup object is the same ThreadGroup or a direct or indirect parent of the specified ThreadGroup; otherwise false.

Parameter
g – A ThreadGroup object.

public final void resume();

public final void resume() method resumes each Thread object that directly or indirectly belongs to this ThreadGroup object by calling its resume() method.

public final void setDaemon(boolean daemon);

public final void setDaemon(boolean daemon) method sets the daemon attribute of this ThreadGroup object to the given value. A daemon thread group is destroyed when the last Thread in it is stopped, or the last ThreadGroup in it is destroyed.

Parameter
daemon – The new value for this ThreadGroup object’s daemon attribute.

public final void setMaxPriority(int pri);

public final void setMaxPriority(int pri) method sets the maximum priority that can be assigned to Thread objects that belong to this ThreadGroup object.

It is possible for a ThreadGroup to contain Thread objects that have higher priorities than this
maximum, if they were given that higher priority before the maximum was set to a lower value.
Parameter
pri – The new maximum priority for Thread objects that belong to this ThreadGroup object.

public final void stop();

public final void stop() method stops each Thread object that directly or indirectly belongs to this ThreadGroup object by calling its stop() method.

public final void suspend();

public final void suspend() method suspends each Thread object that directly or indirectly belongs to this ThreadGroup object by calling its suspend() method.

public String toString();

public String toString() method returns a string representation of this ThreadGroup object.

This method returns a string representation of this ThreadGroup object.

public void uncaughtException(Thread t, Throwable e);

public void uncaughtException(Thread t, Throwable e) method is called when a Thread object that belongs to this ThreadGroup object dies because of an uncaught exception. If this ThreadGroup object has a parent ThreadGroup object, this method just calls the parent’s uncaughtException() method. Otherwise, this method must determine whether the uncaught exception is an instance of ThreadDeath. If it is, nothing is done. If it is not, the method calls the printStackTrace() method of the exception object.

If this method is overridden, the overriding method should end with a call to super.uncaughtException().

Parameter
t – A reference to a Thread that just died because of an uncaught exception.
e – The uncaught exception.
Below is an example how to create a thread and assign it to the thread group:

Apart from these ThreadGroup class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

ThreadGroup tg=new ThreadGroup();
//creates a  new ThreadGroup 
Thread t=new Thread(tg);
// creates a thread in tg ThreadGroup as a member.
 

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *