List all threads and recursively list all subgroup. - Android java.lang

Android examples for java.lang:Thread

Description

List all threads and recursively list all subgroup.

Demo Code


//package com.java2s;

import android.util.Log;

public class Main {
    /** /* ww w .  ja  va  2s  .  c  o  m*/
     * List all threads and recursively list all subgroup. This method is 
     * taken from the StackOverflow article at: 
     * http://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java
     * 
     * @param group
     * @param indent
     */
    private static void listThreads(ThreadGroup group, String indent) {
        System.out.println(indent + "Group[" + group.getName() + ":"
                + group.getClass() + "]");
        int nt = group.activeCount();
        Thread[] threads = new Thread[nt * 2 + 10]; //nt is not accurate
        nt = group.enumerate(threads, false);

        // List every thread in the group
        for (int i = 0; i < nt; i++) {
            Thread t = threads[i];

            Log.d("THREAD",
                    indent + "  Thread[" + t.getName() + ":" + t.getClass()
                            + "]");
        }

        // Recursively list all subgroups
        int ng = group.activeGroupCount();
        ThreadGroup[] groups = new ThreadGroup[ng * 2 + 10];
        ng = group.enumerate(groups, false);

        for (int i = 0; i < ng; i++) {
            listThreads(groups[i], indent + "  ");
        }
    }
}

Related Tutorials