Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Sep 30, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Get all your Threads and Thread Groups

It might be useful to garner an enumeration of all thread groups and their threads in your running application. The following method shows you how to do that:
public void printOutThreadGroups()
    //create a thread
    Thread t = new Thread();
    //get the current thread
    Thread tt = t.currentThread();
    //get the thread group of current thread
    ThreadGroup tg = tt.getThreadGroup();
    ThreadGroup topMost = null;
    //find the topmost thread group
    while(tg != null)
        topMost = tg;
        tg = tg.getParent();
    //get an estimate of active thread groups under topMost
    int groupCount = topMost.activeGroupCount();
    //get an enumeration of thread groups under topMost
    ThreadGroup[] tgArray = new ThreadGroup[groupCount];
    topMost.enumerate(tgArray, true);
    //for every thread group under topMost, print out the 
active threads System.out.println("Top Most ThreadGroup: \""+topMost.getName()+ "\" has the following Thread Groups:"); System.out.println("**********************"); for(int i=0; i<tgArray.length; i++) { ThreadGroup aThreadGroup = tgArray[i]; int count = aThreadGroup.activeCount(); //get an enumeration of threads in aThreadGroup Thread[] tArray = new Thread[count]; aThreadGroup.enumerate(tArray, true); System.out.println("Thread Group: \""+aThreadGroup.getName()+ "\" has the following threads: "); String indent = " "; for(int j=0; j<tArray.length; j++) { Thread aThread = tArray[j]; System.out.println(indent +" \""+aThread.getName()+"\""); indent += " "; } System.out.println("**********************"); tArray = null; } tgArray = null; t = null; }
Behrouz Fallahi
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date