dump Thread Group - Java java.lang

Java examples for java.lang:Thread

Description

dump Thread Group

Demo Code


import java.io.*;

public class Main{
    public static Writer dumpThreadGroup(Writer out,
            ThreadGroup threadGroup, int index, boolean recurse, boolean xml)
            throws Exception {
        int activeCount = threadGroup.activeCount();
        StringBuilder sb = new StringBuilder(100);
        if (xml) {
            String s = threadGroup.getName().replace('<', '{')
                    .replace('>', '}');
            sb.append("<ThreadGroup name=\"").append(s)
                    .append("\" count=\"").append(activeCount)
                    .append("\">");
            out.write(sb.toString());//from   w  w  w  .  java 2s .  com
        } else {
            sb.append("ThreadGroup(").append(index).append(")[");
            out.write(sb.toString());
            out.write(threadGroup.getName());
            out.write("]\n");
        }

        Thread[] threads = new Thread[activeCount];

        int num = threadGroup.enumerate(threads, false);

        for (int t = 0; t < num; t++) {
            if (threads[t] == null) {
                continue;
            }

            sb.setLength(0);
            if (xml) {
                String s = threads[t].getName().replace('<', '{')
                        .replace('>', '}');
                sb.append("<Thread name=\"").append(s).append("\"/>");
                out.write(sb.toString());
            } else {
                out.write(StringHelper.spaces(3));
                sb.append("Thread(").append(t).append(")[");
                out.write(sb.toString());
                out.write(threads[t].getName());
                out.write("]\n");
            }
        }

        if (recurse) {
            ThreadGroup[] groups = new ThreadGroup[threadGroup
                    .activeGroupCount()];

            num = threadGroup.enumerate(groups, false);

            for (int g = 0; g < num; g++) {
                if (groups[g] != null) {
                    ThreadHelper.dumpThreadGroup(out, groups[g], g,
                            recurse, xml);
                }
            }
        }

        if (xml) {
            out.write("</ThreadGroup>");
        }

        return out;
    }
}

Related Tutorials