Thread Group Tree Navigation
Home ] Up ] The Default Thread Group ] Creating a Thread Group ] Thread Group Methods ] [ Thread Group Tree Navigation ] An Example ]

 

 

ThreadGroup contains:

  • A set of enumerate() methods which allow you to discover what threads and other thread groups are members of this ThreadGroup.
  • A getParent() method to determine the parent thread group of this ThreadGroup
  • A parentOf() method to determine whether this thread group is the parent of the specified thread group.
  • An activeCount() method to determine the number of threads that are members of this thread group tree (recursively)
  • An activeGroupCount() method to return the number of thread groups that are members of this thread group tree (recursively)

Here's an example of a Java application that reports on all the thread groups and threads it can find within its JVM:

package threads;

public class TestThreadGroup extends Thread
{
    public static void main(String[] args)
    {
        System.out.println(doWork());
    }
    
    static String doWork()
    {
        String s = dumpThreadGroupTree();
        createThreadGroups();
        s += dumpThreadGroupTree();
        
        return s;
    }
    
    static String dumpThreadGroupTree()
    {
        ThreadGroup root = getRoot();
        String s = "------Dump of ThreadGroup tree-----\n";
        s += dump(root);
        
        return s;
    }
    
    static void createThreadGroups()
    {
        ThreadGroup parentGroup = 
                new ThreadGroup("My New Parent ThreadGroup");
        ThreadGroup childGroup = 
                new ThreadGroup(parentGroup, 
                                "My New Child ThreadGroup");
        Thread thread1 = new TestThreadGroup(parentGroup, 
                                             "My First Thread");
        parentGroup.setMaxPriority(7);
        Thread thread2 = new TestThreadGroup(parentGroup, 
                                             "My Second Thread");
        Thread thread3 = new TestThreadGroup(childGroup, 
                                             "My Third Thread");
        childGroup.setMaxPriority(5);
        Thread thread4 = new TestThreadGroup(childGroup, 
                                             "My Fourth Thread");
    }
    
    TestThreadGroup(ThreadGroup tg, String name)
    {
        super(tg, name);
    }
    
    public void run()
    {
        for (int i = 0; i < 10000; i++)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                // Ignore
            }
        }
    }
    
    static ThreadGroup getRoot()
    {
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
        while (true)
        {
            ThreadGroup parent = tg.getParent();
            if (parent == null)
                break;
            tg = parent;
        }
        
        return tg;
    }
    
    static String dump(ThreadGroup tg)
    {
        String s = "";
        String indent = "";
        for (int i = 0; i < m_indent; i++)
            indent += ONE_INDENT;
            
        s += indent + "ThreadGroup \"" + tg.getName() + "\"\n";
        s += indent + " Max priority : " + tg.getMaxPriority() + "\n";
        s += indent + " Daemon : " + tg.isDaemon() + "\n";
        
        // Dump the threads in this thread group
        m_indent++;
        // overestimate size
        Thread[] threads = new Thread[tg.activeCount()];
        tg.enumerate(threads);
        for (int i = 0; i < threads.length; i++)
        {
            Thread t = threads[i];
            if (t == null)
                break;
            if (t.getThreadGroup() == tg)
                s += dump(t);
        }
        
        // Dump the thread groups in this thread group
        ThreadGroup[] threadGroups = 
                new ThreadGroup[tg.activeGroupCount()]; 
                                     // overestimate size
        tg.enumerate(threadGroups);
        for (int i = 0; i < threadGroups.length; i++)
        {
            ThreadGroup g = threadGroups[i];
            if (g == null)
                break;
            if (g.getParent() == tg)
                s += dump(g);
        }
        --m_indent;
        
        return s;
    }
    
    static String dump(Thread t)
    {
        String s = "";
        String indent = "";
        for (int i = 0; i < m_indent; i++)
            indent += ONE_INDENT;
            
        s += indent + "Thread \"" + t.getName() + "\"\n";
        s += indent + " Priority : " + t.getPriority() + "\n";
        s += indent + " Daemon : " + t.isDaemon() + "\n";
        
        return s;
    }
    
    //// Private data ////////
    private static int m_indent = 0;
    private static final String ONE_INDENT = "  ";
}

and here's what it outputs on my machine, with the JVM I used:

------Dump of ThreadGroup tree-----
ThreadGroup "system"
 Max priority : 10
 Daemon : false
  Thread "Finalizer thread"
   Priority : 1
   Daemon : true
  Thread "SymcJIT-LazyCompilation-0"
   Priority : 1
   Daemon : true
  Thread "SymcJIT-LazyCompilation-PA"
   Priority : 10
   Daemon : true
  ThreadGroup "main"
   Max priority : 10
   Daemon : false
    Thread "main"
     Priority : 5
     Daemon : false
------Dump of ThreadGroup tree-----
ThreadGroup "system"
 Max priority : 10
 Daemon : false
  Thread "Finalizer thread"
   Priority : 1
   Daemon : true
  Thread "SymcJIT-LazyCompilation-0"
   Priority : 1
   Daemon : true
  Thread "SymcJIT-LazyCompilation-PA"
   Priority : 10
   Daemon : true
  ThreadGroup "main"
   Max priority : 10
   Daemon : false
    Thread "main"
     Priority : 5
     Daemon : false
    ThreadGroup "My New Parent ThreadGroup"
     Max priority : 7
     Daemon : false
      Thread "My First Thread"
       Priority : 5
       Daemon : false
      Thread "My Second Thread"
       Priority : 5
       Daemon : false
      ThreadGroup "My New Child ThreadGroup"
       Max priority : 5
       Daemon : false
        Thread "My Third Thread"
         Priority : 5
         Daemon : false
        Thread "My Fourth Thread"
         Priority : 5
         Daemon : false
 
The page was last updated February 19, 2008