Example usage for java.lang Thread getAllStackTraces

List of usage examples for java.lang Thread getAllStackTraces

Introduction

In this page you can find the example usage for java.lang Thread getAllStackTraces.

Prototype

public static Map<Thread, StackTraceElement[]> getAllStackTraces() 

Source Link

Document

Returns a map of stack traces for all live threads.

Usage

From source file:ThreadDemo.java

public static void main(String args[]) {

    ThreadDemo trace = new ThreadDemo();
    Thread t = new Thread(trace);

    // this will call run() method
    t.start();/*from   w ww. ja  v  a2  s  .c  om*/

    // returns a map of stack traces
    Map m = Thread.getAllStackTraces();
}

From source file:org.hobbit.core.run.ComponentStarter1.java

/**
 * This is the main method creating and starting an instance of a
 * {@link Component} with the given class name.
 * //w  w  w  . j  a v  a  2 s . c om
 * @param args
 *            The first element has to be the class name of the component.
 */
public static void main(String[] args) {
    if (args.length < 1) {
        LOGGER.error("Not enough arguments. The name of a class implementing the "
                + Component.class.getCanonicalName() + " interface was expected.");
        System.exit(ERROR_EXIT_CODE);
    }
    addShutdownHook();
    boolean success = true;
    try {
        component = createComponentInstance(args[0]);
        // initialize the component
        component.init();
        // run the component
        component.run();
    } catch (Throwable t) {
        LOGGER.error("Exception while executing component. Exiting with error code.", t);
        success = false;
    } finally {
        LOGGER.info("Closing component...");
        closeComponent();
        LOGGER.info("Component closed.");
    }

    if (!success) {
        System.exit(ERROR_EXIT_CODE);
    } else {
        Map<Thread, StackTraceElement[]> activeThreads = Thread.getAllStackTraces();
        for (Thread t : activeThreads.keySet()) {
            StackTraceElement[] stes = activeThreads.get(t);
            LOGGER.info("Thread {} ({}): {}", t.getName(), (t.isAlive() ? "alive" : "not alive"),
                    (stes.length > 0 ? stes[0].toString() : " no stack trace "));
        }
        LOGGER.info("I am a workaround and will force termination now. I would like to be replaced.");
        System.exit(0);
    }
}

From source file:Main.java

/**
 * @return the currently-live threads./*from   www .j ava2 s .c o m*/
 */
public static Set<Thread> getCurrentThreads() {
    return Thread.getAllStackTraces().keySet();
}

From source file:Main.java

public static Map<State, Map<Thread, StackTraceElement[]>> getAllStackTracesByState() {
    final Map<Thread, StackTraceElement[]> t = Thread.getAllStackTraces();

    final Map<State, Map<Thread, StackTraceElement[]>> running = new HashMap<Thread.State, Map<Thread, StackTraceElement[]>>();

    for (final Entry<Thread, StackTraceElement[]> i : t.entrySet()) {
        final State state = i.getKey().getState();
        if (running.get(state) == null) {
            running.put(state, new HashMap<Thread, StackTraceElement[]>());
        }//from  w  w w. j  a  v a  2s  .c o m
        StackTraceElement[] stackTrace = i.getValue();
        if (stackTrace == null) {
            stackTrace = new StackTraceElement[0];
        }

        running.get(state).put(i.getKey(), stackTrace);
    }

    return running;
}

From source file:Main.java

public static <T> List<T> allExistedThreadLocalValues(ThreadLocal<T> variable) {
    Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
    List<T> result = new ArrayList<T>();
    Set<Thread> allThreads = allStackTraces.keySet();
    for (Thread thread : allThreads) {
        T v = getValue(variable, thread);
        if (v != null)
            result.add(v);//from ww  w  .j a  v a2s. co  m
    }
    return result;
}

From source file:Main.java

public static void dumpThreads() {
    final Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
    for (Thread thread : threads.keySet())
        System.out.println((thread.isDaemon() ? "DAEMON  " : "        ") + thread.getName());

    // final ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads(false, false);
    //        for(ThreadInfo thread : threads) {
    //            System.out.println(thread.getThreadName() + ": ");
    //        }//from  w w  w. j  a  v a2s.  c  om
}

From source file:Main.java

public static Set<Thread> getThreads() {
    return Thread.getAllStackTraces().keySet();
}

From source file:Main.java

/**
 * @return a list of live threads// w w  w. ja  va2s  .  com
 */
public static List<Thread> getLiveThreads() {
    List<Thread> ret = new ArrayList<Thread>();
    ret.addAll(Thread.getAllStackTraces().keySet());
    return ret;
}

From source file:Main.java

public static void processThreadDump(final Consumer<String> writer) {
    for (final Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
        writer.accept("Thread: " + entry.getKey());
        final StackTraceElement[] trace = entry.getValue();
        for (final StackTraceElement element : trace) {
            writer.accept("\tat " + element);
        }//from w  ww.j  a v a2s  .  c o m
    }
}

From source file:Main.java

public static void dumpAllStackTraces() {
    synchronized (System.err) {
        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        for (Map.Entry<Thread, StackTraceElement[]> entry : allStackTraces.entrySet()) {
            System.err.println("Stack Trace for " + entry.getKey().getName());
            StackTraceElement[] elements = entry.getValue();
            for (StackTraceElement element : elements) {
                System.err.println("\tat " + element.toString());
            }//  w w w  .  ja v  a 2 s .com
            System.err.println();
        }
    }
}