Example usage for java.lang Thread getState

List of usage examples for java.lang Thread getState

Introduction

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

Prototype

public State getState() 

Source Link

Document

Returns the state of this thread.

Usage

From source file:Main.java

/**
 * Waits till thread is in state./*ww w. j  av  a  2  s  .co m*/
 *
 * @param thread the tread to monitor
 * @param state the state to wait for
 * @throws InterruptedException if the calling thread gets interrupted
 */
public static void waitTillThreadInState(Thread thread, Thread.State state) throws InterruptedException {
    while (thread.getState() != state) {
        TimeUnit.MILLISECONDS.sleep(1L);
    }
}

From source file:Main.java

/**
 * This method recursively visits all thread groups under group.
 * /*ww w. j  a va  2 s.c  om*/
 * @param group
 *            {@link ThreadGroup}
 * @param level
 *            a thread level
 */
public static void viewRunningThreads(ThreadGroup group, int level) {
    /* Get threads in group */
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);

    /* Enumerate each thread in group */
    for (int i = 0; i < numThreads; i++) {
        // Get thread
        Thread thread = threads[i];
        System.out.println(thread.getState() + ", " + thread.getName());
    }

    /* Get thread subgroups of group */
    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);

    /* Recursively visit each subgroup */
    for (int i = 0; i < numGroups; i++) {
        viewRunningThreads(groups[i], level + 1);
    }
}

From source file:Main.java

public static String dumpStack(Thread t) {
    StringBuilder sb = new StringBuilder();
    sb.append(t.getName() + "[" + t.getId() + "] - " + t.getState() + ":\n");
    for (StackTraceElement ste : t.getStackTrace()) {
        sb.append("\tat " + ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getFileName() + ":"
                + ste.getLineNumber() + ")\n");
    }// www  .  j av a2 s .  c  o m
    return sb.toString();
}

From source file:Main.java

/**
 * Returns the number of threads that are in state {@link State#RUNNABLE} in
 * in the specified array of threads..//from  w  w w  .java2  s  .co m
 * 
 * @return the number of threads that are in state {@link State#RUNNABLE} in
 *         the specified array of threads.
 */
public static int countActive(Thread[] threads) {
    int result = 0;

    for (Thread t : threads) {
        if (t.getState() == State.RUNNABLE) {
            result++;
        }
    }

    return result;
}

From source file:Main.java

public static String getThreadInfo(Thread thread) {
    String info = String.format("id:%s, Name:%s, State:%s , Status:%d", thread.getId(), thread.getName(),
            thread.getState(), thread.getPriority());
    return info;//from   ww w.ja  v a  2 s  .c o  m
}

From source file:Main.java

/**
 * Returns the number of threads that are in state
 * {@link State#TIMED_WAITING}, {@link State#WAITING} or
 * {@link State#BLOCKED} in the specified array of threads.
 * /*from  ww  w  . j  a va2  s . co  m*/
 * @param threads
 *            threads to consider.
 * 
 * @return the number of threads that are in state
 *         {@link State#TIMED_WAITING}, {@link State#WAITING} or
 *         {@link State#BLOCKED} in the specified array of threads.
 */
public static int countWaiting(Thread[] threads) {
    int result = 0;

    for (Thread t : threads) {
        if (t.getState() == State.TIMED_WAITING || t.getState() == State.WAITING
                || t.getState() == State.BLOCKED) {
            result++;
        }
    }

    return result;
}

From source file:Main.java

/**
 * @param thread a thread/*from   w w w  . ja  va  2 s . c  o  m*/
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
    Throwable t = new Throwable(
            String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
    t.setStackTrace(thread.getStackTrace());
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Main.java

/**
 * Get a list of all threads with a given thread state.
 * Thread states are defined in the Thread.State enum for
 * the Thread class.  Principal thread states include
 * RUNNABLE, WAITING, TIMED_WAITING, and BLOCKED.  An
 * empty array is returned if there are no threads in
 * the chosen state.//from  w w w. j av  a2 s  .  c  o  m
 *
 * @param   state   the state to look for
 * @return      an array of threads in that state
 */
public static Thread[] getAllThreads(final Thread.State state) {
    final Thread[] allThreads = getAllThreads();
    final Thread[] found = new Thread[allThreads.length];
    int nFound = 0;
    for (Thread thread : allThreads)
        if (thread.getState() == state)
            found[nFound++] = thread;
    return java.util.Arrays.copyOf(found, nFound);
}

From source file:Main.java

/**
 * Get a list of all threads with a given thread state.
 * Thread states are defined in the Thread.State enum for
 * the Thread class.  Principal thread states include
 * RUNNABLE, WAITING, TIMED_WAITING, and BLOCKED.  An
 * empty array is returned if there are no threads in
 * the chosen state.//from   w  ww.  j  a  v a2s  .c  o m
 *
 * @param   state   the state to look for
 * @return      an array of threads in that state
 */
public static Thread[] getAllThreads(final Thread.State state) {
    final Thread[] allThreads = getAllThreads();
    final Thread[] found = new Thread[allThreads.length];
    int nFound = 0;
    for (Thread thread : allThreads)
        if (thread.getState() == state)
            found[nFound++] = thread;
    return found.clone();
}

From source file:Main.java

/**
 * Print a stack trace of the current thread.
 *//*from www.j a va2 s .c  o m*/
public static void printFullStackTrace() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();

    for (StackTraceElement e : backtrace) {
        System.out.printf("    Stack Trace: %s\n", e);
    }
}