Example usage for java.lang Thread isAlive

List of usage examples for java.lang Thread isAlive

Introduction

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

Prototype

public final native boolean isAlive();

Source Link

Document

Tests if this thread is alive.

Usage

From source file:Main.java

public static void awaitForTermination(List<Thread> threads, long timeout, TimeUnit timeUnit) {
    try {//from  ww  w. j a  v  a  2 s  .  c o  m
        long finishTime = System.currentTimeMillis() + timeUnit.toMillis(timeout);
        for (Thread t : threads) {
            long now = System.currentTimeMillis();
            if (now > finishTime) {
                throw new IllegalStateException("failed to stop all threads");
            } else {
                t.join(finishTime - now);
                if (t.isAlive()) {
                    throw new IllegalStateException("failed to stop all threads");
                }
            }
        }
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
}

From source file:stroom.streamstore.server.fs.ManualCheckStreamPerformance.java

public static void averageTimeCheck(final String msg, final TimedAction provider) throws IOException {
    final HashMap<Thread, Long> threadTimes = new HashMap<>();
    for (int i = 0; i < testThreadCount; i++) {
        final Thread t = new Thread((() -> {
            try {
                threadTimes.put(Thread.currentThread(), provider.newTimedAction());
            } catch (final IOException e) {
                e.printStackTrace();//from  ww w .  java  2  s  .c  om
            }
        }));

        threadTimes.put(t, 0L);
        t.start();
    }
    boolean running = false;
    do {
        ThreadUtil.sleep(1000);

        running = false;
        for (final Thread thread : threadTimes.keySet()) {
            if (thread.isAlive()) {
                running = true;
                break;
            }
        }
    } while (running);

    long totalTime = 0;
    for (final Thread thread : threadTimes.keySet()) {
        totalTime += threadTimes.get(thread);
    }
    final long average = totalTime / threadTimes.size();

    System.out.println(
            "Average for " + StringUtils.leftPad(msg, 20) + " is " + StringUtils.leftPad("" + average, 10));
}

From source file:Main.java

/** Collect info about a thread  */
private static void collectThreadInfo(final Thread t, final StringBuffer str, final String indent) {
    if (t == null) {
        return;// w ww.j a va 2s.c o  m
    }
    str.append(indent).append("Thread: [").append(t.getName()).append("]  Priority: [").append(t.getPriority())
            .append(t.isDaemon() ? " Daemon" : "").append("] ").append(t.isAlive() ? "" : " Not Alive")
            .append(LINE_SEPARATOR);
}

From source file:TimeoutController.java

/**
 * Executes <code>task</code>. Waits for <code>timeout</code>
 * milliseconds for the task to end and returns. If the task does not return
 * in time, the thread is interrupted and an Exception is thrown.
 * The caller should override the Thread.interrupt() method to something that
 * quickly makes the thread die or use Thread.isInterrupted().
 * @param task The thread to execute//from  w  w  w  . j  av  a2 s .  com
 * @param timeout The timeout in milliseconds. 0 means to wait forever.
 * @throws TimeoutException if the timeout passes and the thread does not return.
 */
public static void execute(Thread task, long timeout) throws TimeoutException {
    task.start();
    try {
        task.join(timeout);
    } catch (InterruptedException e) {
        /* if somebody interrupts us he knows what he is doing */
    }
    if (task.isAlive()) {
        task.interrupt();
        throw new TimeoutException();
    }
}

From source file:com.ery.estorm.util.Threads.java

/**
 * Shutdown passed thread using isAlive and join.
 * /* w  ww.  java2 s .c o m*/
 * @param joinwait
 *            Pass 0 if we're to wait forever.
 * @param t
 *            Thread to shutdown
 */
public static void shutdown(final Thread t, final long joinwait) {
    if (t == null)
        return;
    while (t.isAlive()) {
        try {
            t.join(joinwait);
        } catch (InterruptedException e) {
            LOG.warn(t.getName() + "; joinwait=" + joinwait, e);
        }
    }
}

From source file:com.ery.estorm.util.Threads.java

/**
 * @param t//from   w  w w  . j av a  2 s .  c  o  m
 *            Waits on the passed thread to die dumping a threaddump every minute while its up.
 * @throws InterruptedException
 */
public static void threadDumpingIsAlive(final Thread t) throws InterruptedException {
    if (t == null) {
        return;
    }

    while (t.isAlive()) {
        t.join(60 * 1000);
        if (t.isAlive()) {
            ReflectionUtils.printThreadInfo(new PrintWriter(System.out),
                    "Automatic Stack Trace every 60 seconds waiting on " + t.getName());
        }
    }
}

From source file:org.archive.crawler.framework.ToePool.java

private static boolean isAllAlive(Thread[] threads) {
    for (Thread t : threads) {
        if ((t != null) && (!t.isAlive())) {
            return false;
        }//from   w w w . ja va  2 s  . c o  m
    }
    return true;
}

From source file:cam.cl.kilo.RESTBarcode.java

/**
 * Make different API calls according to the item's type
 *
 * @param barcodeNo The item's barcode number
 * @param barcodeType The type of the barcode (ISBN, UPC or other)
 * @return ItemInfo object with item information
 *//*from  w  ww.  ja  v a 2s. c  om*/
public static ItemInfo populateItemInfo(String barcodeNo, String barcodeType) {
    ItemInfo info = new ItemInfo();

    Thread tAmzn = new Thread(new AmznItemLookup(barcodeNo, barcodeType, info));
    Thread tGR = new Thread(new GoodReadsLookup(barcodeNo, barcodeType, info));

    tAmzn.start();

    if ("ISBN".equals(barcodeType))
        tGR.start();

    while (tAmzn.isAlive() || tGR.isAlive())
        ;

    if (!"ISBN".equals(barcodeType)) {
        Thread tOMDB = new Thread(new OMDBLookup(barcodeNo, barcodeType, info));
        while (tOMDB.isAlive())
            ;
    }

    return info;
}

From source file:com.googlecode.icegem.cacheutils.common.Utils.java

/**
 * Executes the thread with specified timeout.
 * //from w w w .  j  av  a  2  s. co  m
 * @param thread
 *            - the thread to execute.
 * @param timeout
 *            - the timeout.
 */
public static void execute(Thread thread, long timeout) {
    thread.start();

    try {
        thread.join(timeout);
    } catch (InterruptedException e) {
        // Should not be interrupted normally.
    }

    if (thread.isAlive()) {
        thread.interrupt();
    }
}

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   ww  w.  java  2  s  . com
        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;
}