Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.felix.webconsole.internal.misc;
 
 import java.net.URL;
This is a helper class for dumping thread stacks.
 
 public class ThreadDumper
 {
 
     private final Method getStackTrace;
     private final Method getId;

    
Base constructor.
 
     public ThreadDumper()
     {
         Method _getStackTrace = null;
         Method _getId = null;
         final Class[] nullArgs = null;
         try
         {
             _getStackTrace = Thread.class.getMethod("getStackTrace"nullArgs); //$NON-NLS-1$
             _getId = Thread.class.getMethod("getId"nullArgs); //$NON-NLS-1$
         }
         catch (Throwable e)
         {
             /* ignore - stack traces will be unavailable */
         }
          = _getStackTrace;
          = _getId;
     }

    
Prints all available thread groups, threads and a summary of threads availability. The thread groups and the threads will be sorted alphabetically regardless of the case.

Parameters:
pw the writer where to print the threads information
withStackTrace to include or not the stack traces
 
     public final void printThreads(PrintWriter pwboolean withStackTrace)
     {
         // first get the root thread group
         ThreadGroup rootGroup = getRootThreadGroup();
 
         // enumerate all other threads
         int numGroups = rootGroup.activeGroupCount();
         final ThreadGroup[] groups = new ThreadGroup[2 * numGroups];
         numGroups = rootGroup.enumerate(groups);
         Arrays.sort(groups, ThreadGroupComparator.getInstance());
 
         printSummary(pwrootGroupgroups);
         printThreadGroup(pwrootGroupwithStackTrace);
 
         // don't use numGroups, but groups.length, otherwise when we get null elements
         // sorted at the beginning, we will skip the real objects
         for (int i = 0; i < groups.lengthi++)
         {
             printThreadGroup(pwgroups[i], withStackTrace);
         }
 
         pw.println();
     }

    
Prints information for the given thread.

Parameters:
pw the writer where to print the threads information
thread the thread for which to print the information
withStackTrace to include or not the stack traces
 
    public final void printThread(PrintWriter pwThread threadboolean withStackTrace)
    {
        if (thread != null)
        {
            pw.print("  Thread ");
            pw.print(getId(thread));
            pw.print('/');
            pw.print(thread.getName());
            pw.print(" ["); //$NON-NLS-1$
            pw.print("priority=");
            pw.print(thread.getPriority());
            pw.print(", alive=");
            pw.print(thread.isAlive());
            pw.print(", daemon=");
            pw.print(thread.isDaemon());
            pw.print(", interrupted=");
            pw.print(thread.isInterrupted());
            pw.print(", loader=");
            pw.print(thread.getContextClassLoader());
            pw.println(']');
            if (withStackTrace)
            {
                printClassLoader(pwthread.getContextClassLoader());
                printStackTrace(pwgetStackTrace(thread));
                pw.println();
            }
        }
    }
    private final void printThreadGroup(PrintWriter pwThreadGroup group,
        boolean withStackTrace)
    {
        if (group != null)
        {
            pw.print("ThreadGroup ");
            pw.print(group.getName());
            pw.print(" ["); //$NON-NLS-1$
            pw.print("maxprio=");
            pw.print(group.getMaxPriority());
            pw.print(", parent=");
            if (group.getParent() != null)
            {
                pw.print(group.getParent().getName());
            }
            else
            {
                pw.print('-');
            }
            pw.print(", isDaemon=");
            pw.print(group.isDaemon());
            pw.print(", isDestroyed=");
            pw.print(group.isDestroyed());
            pw.println(']');
            int numThreads = group.activeCount();
            Thread[] threads = new Thread[numThreads * 2];
            group.enumerate(threadsfalse);
            Arrays.sort(threads, ThreadComparator.getInstance());
            for (int i = 0; i < threads.lengthi++)
            {
                printThread(pwthreads[i], withStackTrace);
            }
            pw.println();
        }
    }
    private final void printClassLoader(PrintWriter pwClassLoader classLoader)
    {
        if (classLoader != null)
        {
            pw.print("    ClassLoader=");
            pw.println(classLoader);
            pw.print("      Parent=");
            pw.println(classLoader.getParent());
            if (classLoader instanceof URLClassLoader)
            {
                URLClassLoader loader = (URLClassLoaderclassLoader;
                URL[] urls = loader.getURLs();
                if (urls != null && urls.length > 0)
                {
                    for (int i = 0; i < urls.lengthi++)
                    {
                        pw.print("      ");
                        pw.print(i);
                        pw.print(" - ");
                        pw.println(urls[i]);
                    }
                }
            }
        }
    }
    private final void printStackTrace(PrintWriter pwObject stackTrace)
    {
        pw.println("    Stacktrace");
        if (stackTrace == null || Array.getLength(stackTrace) == 0)
        {
            pw.println("      -"); //$NON-NLS-1$
        }
        else
        {
            for (int i = 0, len = Array.getLength(stackTrace); i < leni++)
            {
                Object/*StackTraceElement*/stackTraceElement = Array.get(stackTracei);
                pw.print("      "); //$NON-NLS-1$
                pw.println(stackTraceElement);
            }
        }
    }
    private final void printSummary(PrintWriter pwThreadGroup rootGroup,
        ThreadGroup[] groups)
    {
        int alive = 0;
        int daemon = 0;
        int interrupted = 0;
        int threadCount = 0;
        int threadGroupCount = 0;
        int threadGroupDestroyed = 0;
        ArrayList/*<ThreadGroup>*/list = new ArrayList(groups.length + 1);
        list.add(rootGroup);
        list.addAll(Arrays.asList(groups));
        // main group will eventually enumerate ALL threads, so don't
        // count a thread, it if is already processed
        Collection threadSet = new HashSet();
        for (int j = 0; j < list.size(); j++)
        {
            ThreadGroup group = (ThreadGrouplist.get(j);
            if (null == group)
            {
                continue;
            }
            threadGroupCount++;
            if (group.isDestroyed())
            {
                threadGroupDestroyed++;
            }
            Thread[] threads = new Thread[group.activeCount()];
            group.enumerate(threads);
            for (int i = 0, size = threads.lengthi < sizei++)
            {
                Thread thread = threads[i];
                if (null != thread && threadSet.add(thread))
                {
                    if (thread.isAlive())
                    {
                        alive++;
                    }
                    if (thread.isDaemon())
                    {
                        daemon++;
                    }
                    if (thread.isInterrupted())
                    {
                        interrupted++;
                    }
                    threadCount++;
                }
            }
        }
        pw.print("Status: ");
        pw.print(threadCount);
        pw.print(" threads (");
        pw.print(alive);
        pw.print(" alive/");
        pw.print(daemon);
        pw.print(" daemon/");
        pw.print(interrupted);
        pw.print(" interrupted) in ");
        pw.print(threadGroupCount);
        pw.print(" groups (");
        pw.print(threadGroupDestroyed);
        pw.print(" destroyed).");
        pw.println();
        pw.println();
    }
    private final String getId(Thread thread)
    {
        String ret = ""//$NON-NLS-1$
        if (null != )
        {
            try
            {
                ret = "#" + .invoke(threadnull); //$NON-NLS-1$
            }
            catch (Throwable e)
            {
                /* ignore */
            }
        }
        return ret;
    }
    private final Object/*StackTraceElement[]*/getStackTrace(Thread thread)
    {
        Object/*StackTraceElement[]*/ret = null;
        if (null != )
        {
            try
            {
                ret = .invoke(threadnull);
            }
            catch (Throwable e)
            {
                // ignore - no traces available
            }
        }
        return ret;
    }
    private static final ThreadGroup getRootThreadGroup()
    {
        ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
        while (rootGroup.getParent() != null)
        {
            rootGroup = rootGroup.getParent();
        }
        return rootGroup;
    }
final class ThreadComparator implements Comparator
    private ThreadComparator()
    {
        // prevent instantiation
    }
    private static final Comparator instance = new ThreadComparator();
    public static final Comparator getInstance()
    {
        return ;
    }
    public int compare(Object thread1Object thread2)
    {
        if (thread1 == null && thread2 == null)
        {
            return 0;
        }
        if (thread1 == null)
        {
            return 1; // null is always >, moves nulls at the end
        }
        if (thread2 == null)
        {
            return -1; // first is always < than null, moves nulls at the end 
        }
        String t1 = ((Threadthread1).getName();
        String t2 = ((Threadthread2).getName();
        if (null == t1)
        {
            t1 = ""//$NON-NLS-1$
        }
        if (null == t2)
        {
            t2 = ""//$NON-NLS-1$
        }
        return t1.toLowerCase().compareTo(t2.toLowerCase());
    }
final class ThreadGroupComparator implements Comparator
    private ThreadGroupComparator()
    {
        // prevent instantiation
    }
    private static final Comparator instance = new ThreadGroupComparator();
    public static final Comparator getInstance()
    {
        return ;
    }
    public int compare(Object thread1Object thread2)
    {
        if (thread1 == null && thread2 == null)
        {
            return 0;
        }
        if (thread1 == null)
        {
            return 1; // null is always >, moves nulls at the end
        }
        if (thread2 == null)
        {
            return -1; // first is always < than null, moves nulls at the end 
        }
        String t1 = ((ThreadGroupthread1).getName();
        String t2 = ((ThreadGroupthread2).getName();
        if (null == t1)
        {
            t1 = ""//$NON-NLS-1$
        }
        if (null == t2)
        {
            t2 = ""//$NON-NLS-1$
        }
        return t1.toLowerCase().compareTo(t2.toLowerCase());
    }
New to GrepCode? Check out our FAQ X