Java Thread Dump dumpThreadGroup(ThreadGroup tg, StringBuffer sb)

Here you can find the source of dumpThreadGroup(ThreadGroup tg, StringBuffer sb)

Description

Dump threads in threadgroup recursively.

License

Open Source License

Parameter

Parameter Description
tg Root of thread group tree.
buffer String buffer to append output

Declaration

static void dumpThreadGroup(ThreadGroup tg, StringBuffer sb) 

Method Source Code

//package com.java2s;
/*/*from   www . java  2s  .  c o  m*/
 * @(#)ConsoleHelper.java   1.11 10/03/24
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /**
     * Dump threads in threadgroup recursively.
     *
     * @param tg Root of thread group tree.
     * @param buffer String buffer to append output
     */
    static void dumpThreadGroup(ThreadGroup tg, StringBuffer sb) {
        if (tg != null) {
            // Clean up thread group if necessary.
            try {
                if (tg.activeCount() == 0 && tg.activeGroupCount() == 0 && tg.isDestroyed() == false)
                    tg.destroy();
            } catch (Throwable e) {
            }

            // Dump thread group
            sb.append("Group " + tg.getName());
            sb.append(",ac=" + tg.activeCount());
            sb.append(",agc=" + tg.activeGroupCount());
            sb.append(",pri=" + tg.getMaxPriority());
            if (tg.isDestroyed())
                sb.append(",destoyed");
            if (tg.isDaemon())
                sb.append(",daemon");
            sb.append("\n");

            Thread[] tt = new Thread[1000];
            tg.enumerate(tt, false);

            // Dump threads within thread group
            for (int i = 0; i < tt.length; i++) {
                if (tt[i] != null) {
                    sb.append("    ");
                    sb.append(tt[i].getName());
                    sb.append(",");
                    sb.append(tt[i].getPriority());

                    if (tt[i].isAlive())
                        sb.append(",alive");
                    else
                        sb.append(",not alive");

                    if (tt[i].isDaemon())
                        sb.append(",daemon");

                    if (tt[i].isInterrupted())
                        sb.append(",interrupted");

                    sb.append("\n");
                }
            }

            // Obtain sub-thread group
            ThreadGroup[] tgArray = new ThreadGroup[1000];
            tg.enumerate(tgArray, false);

            // Dump sub-thread group
            for (int i = 0; i < tgArray.length; i++) {
                if (tgArray[i] != null)
                    dumpThreadGroup(tgArray[i], sb);
            }
        }
    }
}

Related

  1. dumpStack(Thread t)
  2. dumpThreads()
  3. dumpThreads()