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

/**
 * Check whether a specific thread is running (or able to run) right now.
 *
 * @param t the thread to check//w  w  w .ja  v a2s .  c o m
 * @return true is the specified thread is or could be running right now
 */
static public boolean canThreadRun(@Nonnull Thread t) {
    Thread.State s = t.getState();
    return s.equals(Thread.State.RUNNABLE);
}

From source file:Main.java

public static void cancelAllTask(Thread... task) {
    for (Thread t : task) {
        if (t != null && (t.getState() == Thread.State.RUNNABLE || t.getState() != Thread.State.TERMINATED)) {
            t.interrupt();//from  w  ww .  j  av a  2s. com
            t = null;
        }
    }
}

From source file:Main.java

/**
 * Check whether a specific thread is currently waiting.
 * <p>/*  www  .  j  a v  a 2 s  .c o m*/
 * Note: This includes both waiting due to an explicit wait() call, and due
 * to being blocked attempting to synchronize.
 * <p>
 * Note: {@link #canThreadRun(Thread)} and {@link #isThreadWaiting(Thread)}
 * should never simultaneously be true, but it might look that way due to
 * sampling delays when checking on another thread.
 *
 * @param t the thread to check
 * @return true is the specified thread is or could be running right now
 */
static public boolean isThreadWaiting(@Nonnull Thread t) {
    Thread.State s = t.getState();
    return s.equals(Thread.State.BLOCKED) || s.equals(Thread.State.WAITING)
            || s.equals(Thread.State.TIMED_WAITING);
}

From source file:Main.java

/**
 * waits until all given threads have terminated.
 *//*from   ww  w  . j ava  2 s  . c  om*/
public static void waitFor(Thread... ts) {
    boolean wait;
    do {
        /*
         * yield(): Causes the thread to temporarily pause 
         * and allow other threads to execute. 
         */
        Thread.yield();

        wait = false;
        for (Thread t : ts) {
            wait = wait || !t.getState().equals(State.TERMINATED);
        }

    } while (wait);
}

From source file:Main.java

/**
 * Determines whether the specified Thread is in a waiting state, guarding against null Object references
 * <p/>/*from   w  w w.j  a  va2  s  . c o  m*/
 * @param thread the Thread to access it's state.
 * @return a boolean value indicating whether the Thread is in a waiting state.  If the Thread Object reference
 * is null, then this method return false, as no Thread is clearly not waiting for anything.
 * @see java.lang.Thread#getState()
 * @see java.lang.Thread.State#WAITING
 */
public static boolean isWaiting(final Thread thread) {
    return (thread != null && thread.getState().equals(State.WAITING));
}

From source file:Main.java

public static boolean areAnyThreadsRunning(ThreadGroup tg) {
    for (Thread t : getThreads(tg)) {
        switch (t.getState()) {
        case RUNNABLE:
            return true;
        }/*w  ww  .  ja v  a2 s  .com*/
    }
    return false;
}

From source file:Main.java

public static boolean areAnyThreadsInTimedWaiting(ThreadGroup tg) {
    for (Thread t : getThreads(tg)) {
        switch (t.getState()) {
        case TIMED_WAITING:
            return true;
        }//  w w  w. j a  v  a2 s.co m
    }
    return false;
}

From source file:Main.java

public static void printStatus(Object o) {
    if (o instanceof Thread) {
        Thread t = (Thread) o;
        System.out.println("Thread " + t.getName() + " " + t.getState());
    } else if (o instanceof ExecutorService) {
        ExecutorService e = (ExecutorService) o;
        System.out.println("Executor " + e.isShutdown() + " " + e.isTerminated());
    }/*from   w  w  w.  j a v  a2 s .  com*/
}

From source file:Main.java

public static boolean areAnyThreadsAlive(ThreadGroup tg) {
    for (Thread t : getThreads(tg)) {
        switch (t.getState()) {
        case NEW:
        case TERMINATED:
            break;
        default:/*from   ww w.  ja v  a 2 s  .com*/
            return true;
        }
    }
    return false;
}

From source file:MyThread.java

static void showThreadStatus(Thread thrd) {
    System.out.println(thrd.getName() + " Alive:" + thrd.isAlive() + " State:" + thrd.getState());
}