Example usage for java.lang Thread interrupt

List of usage examples for java.lang Thread interrupt

Introduction

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

Prototype

public void interrupt() 

Source Link

Document

Interrupts this thread.

Usage

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();
            t = null;//from w  ww  .  j  a v  a2  s  .c  om
        }
    }
}

From source file:org.jkcsoft.java.util.HeartbeatLogger.java

public static void stopThreads() {
    for (Thread thread : threads) {
        if (thread != null) {
            thread.interrupt();
        }// w  ww  .jav  a  2s  .co m
    }
}

From source file:org.openhab.binding.plugwise.internal.PlugwiseUtils.java

public static void stopBackgroundThread(@Nullable Thread thread) {
    if (thread != null) {
        thread.interrupt();
        try {//from  w  w w  .  j  a  v a2s.co m
            thread.join();
        } catch (InterruptedException e) {
            Thread.interrupted();
        }
    }
}

From source file:Main.java

public static boolean join(Thread thread, long milliseconds) {
    try {/*from  www. ja v  a2s .co m*/
        thread.join(milliseconds);
    } catch (InterruptedException exception) {
        return false;
    }
    if (thread.isAlive()) {
        thread.interrupt();
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Puts the current thread to sleep for the specified amount of time.
 *
 * @param sleepTime//from ww  w. j  av  a2s.  c  o m
 *        in milliseconds
 */
public static void sleep(long sleepTime) {

    Thread currentThread = Thread.currentThread();

    try {

        Thread.sleep(sleepTime);

    } catch (InterruptedException e) {

        e.printStackTrace();
        currentThread.interrupt();
    }
}

From source file:com.buaa.cfs.utils.ShutdownThreadsHelper.java

/**
 * @param thread {@link Thread to be shutdown}
 * @param timeoutInMilliSeconds time to wait for thread to join after being
 *                              interrupted
 * @return <tt>true</tt> if the thread is successfully interrupted,
 * <tt>false</tt> otherwise//from  w  ww. j  ava  2  s .c om
 * @throws InterruptedException
 */
public static boolean shutdownThread(Thread thread, long timeoutInMilliSeconds) {
    if (thread == null) {
        return true;
    }

    try {
        thread.interrupt();
        thread.join(timeoutInMilliSeconds);
        return true;
    } catch (InterruptedException ie) {
        LOG.warn("Interrupted while shutting down thread - " + thread.getName());
        return false;
    }
}

From source file:org.apache.hadoop.corona.Utilities.java

/**
 * A realiable way to wait for the thread termination
 * @param thread thread to wait for/*from  ww w  .ja  va2s.  co m*/
 */
public static void waitThreadTermination(Thread thread) {
    while (thread != null && thread.isAlive()) {
        thread.interrupt();
        try {
            thread.join();
        } catch (InterruptedException e) {
        }
    }
}

From source file:au.org.ala.layers.util.BatchConsumer.java

static void end() {
    for (Thread t : threads) {
        t.interrupt();
    }
    ;
}

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 ww.j  a  v  a 2 s  . c  o 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();
    }
}

From source file:sample.multithreading.ImageDownloaderThread.java

public static void removeDownload(ImageDownloaderThread paramImageDownloaderThread) {
    if (paramImageDownloaderThread != null) {
        Thread t = paramImageDownloaderThread.mThread;
        if (null != t)
            t.interrupt();
        sThreadPool.remove(paramImageDownloaderThread);
    }//from  w  w w. ja v  a  2s . c  o m
}