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

/**
 * Print diagnostic info about the current thread.
 *//*from  w  ww  .  java2s . c o m*/
public static void printThreadInfo() {
    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();
    if (backtrace.length > 2) {
        System.out.printf("    trace[2]: %s\n", backtrace[2]);
    }
}

From source file:Main.java

public static void startThreadStatusTimer(final Thread t1) {
    new Timer().schedule(new TimerTask() {
        @Override/*from  ww  w  . ja v  a2  s  .c  om*/
        public void run() {
            System.out.println("\t\t\t\t\t\t\t\t\t\tThread(" + t1.getName() + ") state - " + t1.getState());
        }
    }, 0, 1000);
}

From source file:Main.java

public static void startThreadStatusTimer(final Thread t1, int ms) {
    new Timer().schedule(new TimerTask() {
        @Override//from   www  .j a  va2s.c  o m
        public void run() {
            System.out.println("\t\t\t\t\t\t\t\t\t\tThread(" + t1.getName() + ") state - " + t1.getState());
        }
    }, 0, ms);
}

From source file:org.trafodion.rest.ResourceChecker.java

/**
 * Helper function: print the threads//from  w w  w .ja v  a2 s.  c om
 */
public static void printThreads() {
    Set<Thread> threads = Thread.getAllStackTraces().keySet();
    System.out.println("name; state; isDameon; isAlive; isInterrupted");
    for (Thread t : threads) {
        System.out.println(t.getName() + ";" + t.getState() + ";" + t.isDaemon() + ";" + t.isAlive() + ";"
                + t.isInterrupted());
    }
}

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  a  v a  2  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

public static void awaitThreadState(Thread thread, long maxWaitMillis, Thread.State first,
        Thread.State... rest) {
    EnumSet<Thread.State> set = EnumSet.of(first, rest);
    long deadline = maxWaitMillis + System.currentTimeMillis();
    Thread.State currentState;
    do {/*from w w  w . jav  a 2s. c o m*/
        currentState = thread.getState();
        if (System.currentTimeMillis() > deadline) {
            throw new AssertionError("Timed out waiting for thread state of <" + set + ">: " + thread
                    + " (state = " + thread.getState() + ")");
        }
    } while (!set.contains(currentState));
}

From source file:com.l2jfree.lang.L2Thread.java

public static List<String> getStats(Thread t) {
    List<String> list = new FastList<String>();

    list.add(t.toString() + " - ID: " + t.getId());
    list.add(" * State: " + t.getState());
    list.add(" * Alive: " + t.isAlive());
    list.add(" * Daemon: " + t.isDaemon());
    list.add(" * Interrupted: " + t.isInterrupted());
    for (ThreadInfo info : ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { t.getId() }, true,
            true)) {/*from  w w  w  .  j  a v  a2 s.  co m*/
        for (MonitorInfo monitorInfo : info.getLockedMonitors()) {
            list.add("==========");
            list.add(" * Locked monitor: " + monitorInfo);
            list.add("\t[" + monitorInfo.getLockedStackDepth() + ".]: at " + monitorInfo.getLockedStackFrame());
        }

        for (LockInfo lockInfo : info.getLockedSynchronizers()) {
            list.add("==========");
            list.add(" * Locked synchronizer: " + lockInfo);
        }

        list.add("==========");
        for (StackTraceElement trace : info.getStackTrace())
            list.add("\tat " + trace);
    }

    return list;
}

From source file:Main.java

static Object[] createThreadInfo(Thread thread, Object lock, Thread lockOwner) {

    String lockName = null;/*from  w  w  w  .ja  v a  2  s  . c  o  m*/
    String lockOwnerName = null;

    if (lock != null) {
        lockName = lock.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(lock));
        if (lockOwner != null)
            lockOwnerName = lockOwner.getName();
    }

    return new Object[] { thread.getName(), thread.getState(), lockName, lockOwnerName };
}

From source file:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java

static String buildThreadDump() {
    StringBuilder dump = new StringBuilder();
    Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
    for (Map.Entry<Thread, StackTraceElement[]> e : stackTraces.entrySet()) {
        Thread thread = e.getKey();
        dump.append(String.format("\"%s\" %s prio=%d tid=%d %s\njava.lang.Thread.State: %s", thread.getName(),
                (thread.isDaemon() ? "daemon" : ""), thread.getPriority(), thread.getId(),
                Thread.State.WAITING.equals(thread.getState()) ? "in Object.wait()"
                        : StringUtils.lowerCase(thread.getState().name()),
                Thread.State.WAITING.equals(thread.getState()) ? "WAITING (on object monitor)"
                        : thread.getState()));
        for (StackTraceElement stackTraceElement : e.getValue()) {
            dump.append("\n        at ");
            dump.append(stackTraceElement);
        }//w w  w  .j  av a 2 s. co m
        dump.append("\n");
    }
    return dump.toString();
}

From source file:org.midonet.midolman.Midolman.java

public static void dumpStacks() {
    Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
    for (Thread thread : traces.keySet()) {
        System.err.print("\"" + thread.getName() + "\" ");
        if (thread.isDaemon())
            System.err.print("daemon ");
        System.err.print(String.format("prio=%x tid=%x %s [%x]\n", thread.getPriority(), thread.getId(),
                thread.getState(), System.identityHashCode(thread)));

        StackTraceElement[] trace = traces.get(thread);
        for (StackTraceElement e : trace) {
            System.err.println("        at " + e.toString());
        }//from  ww w  .j a v a 2s.  c  o m
    }
}