2

I want to start a ThreadGroup which contains many threads, but the start() method is not present in the ThreadGroup class. It has a stop() method to stop the thread group though. How can I start the thread group if the start() method is not available?

Please see the below code, I can start thread one by one but cannot start the thread group, because start() method is not present in ThreadGroup class. The requirement is we need to start thread group at the same time, how can this be done?

public class ThreadGroupExample
{
    public static void main(String[] args)
    {
    ThreadGroup thGroup1 = new ThreadGroup("ThreadGroup1");

    /* createting threads and adding into thread grout "thGroup1" */
    Thread1 th1 = new Thread1(thGroup1, "JAVA");
    Thread1 th2 = new Thread1(thGroup1, "JDBC");
    Thread2 th3 = new Thread2(thGroup1, "EJB");
    Thread2 th4 = new Thread2(thGroup1, "XML");

    /* starting all thread one by one */
    th1.start();
    th2.start();
    th3.start();
    th4.start();

    // thGroup1.start();

    thGroup1.stop();

    }
}

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

    @Override
    public void run()
    {
    for (int i = 0; i < 10; i++)
    {
        ThreadGroup tg = getThreadGroup();
        System.out.println(getName() + "\t" + i + "\t" + getPriority()
            + "\t" + tg.getName());
    }
    }
}

class Thread2 extends Thread
{

    Thread2(String name)
    {
    super(name);
    }

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

    @Override
    public void run()
    {
    for (int i = 0; i < 10; i++)
    {
        ThreadGroup tg = getThreadGroup();
        System.out.println(getName() + "\t" + i + "\t" + getPriority()
            + "\t" + tg.getName());
    }
    }
}
Idos
  • 15,053
  • 14
  • 60
  • 75
Manish Kumar
  • 227
  • 4
  • 9
  • A `ThreadGroup` is not going to be able to start all its threads for the simple reason that the threads are actually added to the group only when they are started. Note that the **deprecated and dangerous** `stop()` and `suspend()` methods in `ThreadGroup` merely run the same kind of sequential loop you are trying to avoid anyway. – RealSkeptic Feb 13 '16 at 11:17
  • @RealSkeptic are you sure it is a duplicate though? I stipulate to the fact that the dup is what the OP is after, but the question is about a different topic/issue which is not explained in the dup. Please reconsider this. – Idos Feb 14 '16 at 07:18
  • 1
    @Idos I believe in its current form its an XY problem, in which the actual problem is a duplicate and the `ThreadGroup` question is only secondary to it. Your answer and my comment give enough information to explain why ThreadGroup is irrelevant to solve this problem, but any real answer to the OP would actually be an answer to the dupe. – RealSkeptic Feb 14 '16 at 07:32

1 Answers1

4

From the docs:

A thread group represents a set of threads.

It is not designed to .start() multiple threads at the same time.

You can add Threads to the group, or other ThreadGroups, which can access other Threads' state, but not start a ThreadGroup together. A Thread is allowed to access information about its own ThreadGroup, but not to access information about its ThreadGroup's parent ThreadGroup or any other ThreadGroups.

For some more info about the available functions with example of usage read here.

Idos
  • 15,053
  • 14
  • 60
  • 75