Menu
Topics Index
...
`


Multithreaded Programming >
Siva Nookala - 20 Feb 2017
Thread Groups in Java are used to group threads. For example there are many threads in an application sometimes we may confuse why they are created and what are they doing. If we group related threads it will become easy to identify them. ThreadsGroups can also group related ThreadGroups.

Following is the declaration for java.lang.ThreadGroup class: public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandlerThreadGroups form a tree structure. If we create a ThreadGroup child for a ThreadGroup parent, parent ThreadGroup will become Parent for child ThreadGroup. If a parent ThreadGroup is destroyed all the sub ThreadGroups are destroyed.
Following is the declaration for java.lang.ThreadGroup class:
public class ThreadGroup
   extends Object
      implements Thread.UncaughtExceptionHandler
Constructors :
Constructor Description
ThreadGroup(String name) This constructs a new thread group.
ThreadGroup(ThreadGroup parent, String name) This creates a new thread group.
Following are the ThreadGroup methods :
Methods :
int activeGroupCount() This method returns an estimate of the number of active groups in this thread group.
Method Description
int activeCount() This method returns an estimate of the number of active threads in this thread group.
void checkAccess() This method determines if the currently running thread has permission to modify this thread group.
void destroy() This method Destroys this thread group and all of its subgroups.
int enumerate(Thread[] list) This method Copies into the specified array every active thread in this thread group and its subgroups.
int enumerate(ThreadGroup[] list) This method copies into the specified array references to every active subgroup in this thread group.
int getMaxPriority() This method returns the maximum priority of this thread group.
String getName() This method returns the name of this thread group.
ThreadGroup getParent() This method returns the parent of this thread group.
void interrupt() This method interrupts all threads in this thread group.
boolean isDaemon() This method Tests if this thread group is a daemon thread group.
boolean isDestroyed() This method tests if this thread group has been destroyed.
boolean parentOf(ThreadGroup g) This method tests if this thread group is either the thread group argument or one of its ancestor thread groups.
void setDaemon(boolean daemon) This method changes the daemon status of this thread group.
void setMaxPriority(int pri) This method sets the maximum priority of the group.
String toString() This method returns a string representation of this Thread group.
Example Program :
ThreadGroupDemo
class ThreadGroupDemo
{
    public static void main(String[] args)
    {
        ThreadGroup readingThreads = new ThreadGroup("ReadingThreadGroup"); // LINE A
        for (int i = 1; i <= 3; i++)
        {
            A thread = new A(readingThreads, "Thread-" + i); // LINE B
            thread.start();
        }
        ThreadGroup writingThreads = new ThreadGroup("WritingThreadGroup"); // LINE C
        for (int i = 4; i <= 6; i++)
        {
            B thread = new B(writingThreads, "Thread-" + i); // LINE D
            thread.start();
        }
    }
}

class A extends Thread
{
    A(ThreadGroup tg, String name)
    {
        super(tg, name);
    }

    public void run() // LINE E
    {
        System.out.println("I am " + Thread.currentThread().getName() + " I belong to " + Thread.currentThread().getThreadGroup().getName());
        System.out.println("I am running."); // LINE F
    }
}

class B extends Thread
{

    B(ThreadGroup tg, String name)
    {
        super(tg, name);
    }

    public void run()
    {
        System.out.println("I am " + Thread.currentThread().getName() + " I belong to " + Thread.currentThread().getThreadGroup().getName());
        System.out.println("I am running.");
    }
}
OUTPUT

I am Thread-2 I belong to ReadingThreadGroup
I am running.
I am Thread-4 I belong to WritingThreadGroup
I am running.
I am Thread-6 I belong to WritingThreadGroup
I am running.
I am Thread-1 I belong to ReadingThreadGroup
I am running.
I am Thread-3 I belong to ReadingThreadGroup
I am running.
I am Thread-5 I belong to WritingThreadGroup
I am running.

DESCRIPTION

In the above program we have two class A and B which extends Thread. At LINE A we are created a ThreadGroup ReadingThreadsGroup with reference readingThreads. At LINE B we created three threads for A and added them to readingThreads Group by using the Thread class constructor Thread(ThreadGroup reference, String threadName). At LINE C we created a ThreadGroup WritingThreadsGroup with reference writngThreads. At LINE D we created three threads for B and added them to writngThreads Group. We started all the threads by invoking thread's start method which in turn invokes thread's run method. At LINE E inside run method we are printing the statement the threadname and it's threadgroup. At LINE F we are printing the statement I am running. We implemented the same method in the B class run method as well.

THINGS TO TRY
  • 1. Create one more ThreadGroup coordinatingThreads.
    2. Create three threads to B
    3. Add the created threads to threadgroup and start them.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App