Example usage for java.lang Thread join

List of usage examples for java.lang Thread join

Introduction

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

Prototype

public final synchronized void join(final long millis) throws InterruptedException 

Source Link

Document

Waits at most millis milliseconds for this thread to die.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {

    Thread t = new Thread(new ThreadDemo());
    t.start();/*from   ww  w .j  a  v  a  2s . c o  m*/
    // waits at most 2000 milliseconds for this thread to die.
    t.join(2000);

    // after waiting for 2000 milliseconds...
    System.out.print(t.getName());

    System.out.println(", status = " + t.isAlive());
}

From source file:TryThread.java

public static void main(String[] args) {
    Thread first = new TryThread("A ", "a ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    first.start();/* ww w  .  j ava 2s.  c  om*/
    second.start();
    third.start();
    try {
        first.join(2000); // Wait up to 2 second for thread1 to die
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Thread thread = new MyThread();
    thread.start();/*from w  ww  .  j ava2s  .  c o  m*/

    if (thread.isAlive()) {
        System.out.println("Thread has not finished");
    } else {
        System.out.println("Finished");
    }
    long delayMillis = 5000; // 5 seconds
    thread.join(delayMillis);

    if (thread.isAlive()) {
        System.out.println("thread has not finished");
    } else {
        System.out.println("Finished");
    }
    thread.join();
}

From source file:Main.java

public static void safeJoin(Thread thread) throws InterruptedException {
    thread.join(500);
    thread.interrupt();//from ww w  . j ava 2 s . co  m
    thread.join();
}

From source file:Main.java

public static boolean join(Thread thread, long milliseconds) {
    try {//from   ww  w  .ja  v  a 2 s .c o  m
        thread.join(milliseconds);
    } catch (InterruptedException exception) {
        return false;
    }
    if (thread.isAlive()) {
        thread.interrupt();
        return false;
    }
    return true;
}

From source file:Main.java

public static void join(Thread thread, long millis) {
    try {/*from  w w  w .  ja v a 2s. c om*/
        thread.join(millis);
    } catch (InterruptedException inex) {
        // ignore
    }
}

From source file:Main.java

/**
 * Interrupts a given thread and tries to join it.
 *
 * @param thread            the thread to interrupt.
 * @param joinTimeoutMillis a join timeout.
 *///w w w  .ja  v a 2 s . c  o  m
public static void interruptAndJoin(final Thread thread, final long joinTimeoutMillis) {

    thread.interrupt();
    try {

        thread.join(joinTimeoutMillis);
    } catch (final InterruptedException ignored) {

        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

/**
 * Join the current thread with the specified thread, waiting at most
 * the given number of milliseconds. A time of zero means wait forever.
 * A return value of false indicates that some error occured while
 * waiting or that we were unable to wait because the specified thread
 * was the current thread./*from   w ww . ja va 2  s. c o  m*/
 */
public static boolean join(Thread thread, long time) {
    try {
        if (thread == Thread.currentThread())
            return false;
        thread.join(time);
        return true;
    } catch (Exception ex) {
    }
    return false;
}

From source file:Main.java

/**
 * Attempt to join the thread specified safely.
 *  /*from   w ww.  ja va 2 s  . c om*/
 * @param thread  the thread to join, not null
 * @param timeoutMillis  the timeout in milliseconds
 * @return true if the join succeeded, false if a timeout occurred
 */
public static boolean safeJoin(Thread thread, long timeoutMillis) {
    if (!thread.isAlive()) {
        return true;
    }
    try {
        thread.join(timeoutMillis);
    } catch (InterruptedException e) {
        // clear the interrupted state
        Thread.interrupted();
    }
    return !thread.isAlive();
}

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/* w  w w .ja v  a  2s.  co  m*/
 * @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();
    }
}