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:sample.multithreading.ImageDownloaderThread.java

public static void cancelAll() {
    ImageDownloaderThread[] arrayOfImageDownloaderThread = new ImageDownloaderThread[sPoolWorkQueue.size()];
    sPoolWorkQueue.toArray(arrayOfImageDownloaderThread);
    int len = arrayOfImageDownloaderThread.length;
    for (int j = 0; j < len; j++) {
        Thread t = arrayOfImageDownloaderThread[j].mThread;
        if (null != t) {
            t.interrupt();
        }//from   ww  w.j a va  2 s  .c  o  m
    }
}

From source file:com.ksc.http.timers.ClientExecutionAndRequestTimerTestUtils.java

public static void interruptCurrentThreadAfterDelay(final long delay) {
    final Thread currentThread = Thread.currentThread();
    new Thread() {
        public void run() {
            try {
                Thread.sleep(delay);
                currentThread.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();/*from  ww  w  . j  a v  a2 s  .co m*/
            }
        };
    }.start();
}

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

/**
 * Executes the thread with specified timeout.
 * /*from   www  . j  a  va2  s  .  c  om*/
 * @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.projectsexception.myapplist.iconloader.IconManager.java

/**
 * Cancels all Threads in the ThreadPool
 *//* w  w  w.  j a  v  a 2  s. c  o m*/
public static void cancelAll() {

    /*
     * Creates an array of tasks that's the same size as the task work queue
     */
    IconTask[] taskArray = new IconTask[sInstance.mDownloadWorkQueue.size()];

    // Populates the array with the task objects in the queue
    sInstance.mDownloadWorkQueue.toArray(taskArray);

    // Stores the array length in order to iterate over the array
    int taskArraylen = taskArray.length;

    /*
     * Locks on the singleton to ensure that other processes aren't mutating Threads, then
     * iterates over the array of tasks and interrupts the task's current Thread.
     */
    synchronized (sInstance) {

        // Iterates over the array of tasks
        for (IconTask aTaskArray : taskArray) {

            // Gets the task's current thread
            Thread thread = aTaskArray.mThreadThis;

            // if the Thread exists, post an interrupt to it
            if (null != thread) {
                thread.interrupt();
            }
        }
    }
}

From source file:com.projectsexception.myapplist.iconloader.IconManager.java

/**
 * Stops a download Thread and removes it from the threadpool
 *
 * @param downloaderTask The download task associated with the Thread
 * @param packageName The URL being downloaded
 */// w w w  .j  a  va 2  s.  co  m
static public void removeDownload(IconTask downloaderTask, String packageName) {

    // If the Thread object still exists and the download matches the specified URL
    if (downloaderTask != null && downloaderTask.getPackageName().equals(packageName)) {

        /*
         * Locks on this class to ensure that other processes aren't mutating Threads.
         */
        synchronized (sInstance) {

            // Gets the Thread that the downloader task is running on
            Thread thread = downloaderTask.getCurrentThread();

            // If the Thread exists, posts an interrupt to it
            if (null != thread)
                thread.interrupt();
        }
        /*
         * Removes the download Runnable from the ThreadPool. This opens a Thread in the
         * ThreadPool's work queue, allowing a task in the queue to start.
         */
        sInstance.mDownloadThreadPool.remove(downloaderTask.getDownloadRunnable());
    }
}

From source file:org.openqa.selenium.server.browserlaunchers.AsyncExecute.java

/** Waits the specified timeout for the process to die */
public static int waitForProcessDeath(Process p, long timeout) {
    ProcessWaiter pw = new ProcessWaiter(p);
    Thread waiter = new Thread(pw);
    waiter.start();/*from   www. j a  va  2  s . co  m*/
    try {
        waiter.join(timeout);
    } catch (InterruptedException e) {
        throw new RuntimeException("Bug? Main interrupted while waiting for process", e);
    }
    if (waiter.isAlive()) {
        waiter.interrupt();
    }
    try {
        waiter.join();
    } catch (InterruptedException e) {
        throw new RuntimeException("Bug? Main interrupted while waiting for dead process waiter", e);
    }
    InterruptedException ie = pw.getException();
    if (ie != null) {
        throw new ProcessStillAliveException("Timeout waiting for process to die", ie);
    }
    return p.exitValue();

}

From source file:com.example.android.threadsample.PhotoManager.java

/**
 * Stops a download Thread and removes it from the threadpool
 *
 * @param downloaderTask The download task associated with the Thread
 * @param pictureURL The URL being downloaded
 */// w ww . j  a  v  a 2 s. c  o  m
static public void removeDownload(PhotoTask downloaderTask, URL pictureURL) {

    // If the Thread object still exists and the download matches the specified URL
    if (downloaderTask != null && downloaderTask.getImageURL().equals(pictureURL)) {

        /*
         * Locks on this class to ensure that other processes aren't mutating Threads.
         */
        synchronized (sInstance) {

            // Gets the Thread that the downloader task is running on
            Thread thread = downloaderTask.getCurrentThread();

            // If the Thread exists, posts an interrupt to it
            if (null != thread)
                thread.interrupt();
        }
        /*
         * Removes the download Runnable from the ThreadPool. This opens a Thread in the
         * ThreadPool's work queue, allowing a task in the queue to start.
         */
        sInstance.mDownloadThreadPool.remove(downloaderTask.getHTTPDownloadRunnable());
    }
}

From source file:org.unitime.timetable.solver.remote.SolverRegisterService.java

public static Object execute(Callback callback, long timeout) throws Exception {
    Exec ex = new Exec(callback);
    if (timeout <= 0) {
        ex.run();//from  ww  w . j  av a  2 s.  com
    } else {
        Thread et = new Thread(ex);
        et.start();
        et.join(timeout);
        if (et.isAlive())
            et.interrupt();
    }
    if (ex.getAnswer() != null && ex.getAnswer() instanceof Exception)
        throw (Exception) ex.getAnswer();
    return ex.getAnswer();
}

From source file:com.example.android.threadsample.PhotoManager.java

/**
 * Cancels all Threads in the ThreadPool
 *///from   w w  w .java2  s.  com
public static void cancelAll() {

    /*
     * Creates an array of tasks that's the same size as the task work queue
     */
    PhotoTask[] taskArray = new PhotoTask[sInstance.mDownloadWorkQueue.size()];

    // Populates the array with the task objects in the queue
    sInstance.mDownloadWorkQueue.toArray(taskArray);

    // Stores the array length in order to iterate over the array
    int taskArraylen = taskArray.length;

    /*
     * Locks on the singleton to ensure that other processes aren't mutating Threads, then
     * iterates over the array of tasks and interrupts the task's current Thread.
     */
    synchronized (sInstance) {

        // Iterates over the array of tasks
        for (int taskArrayIndex = 0; taskArrayIndex < taskArraylen; taskArrayIndex++) {

            // Gets the task's current thread
            Thread thread = taskArray[taskArrayIndex].mThreadThis;

            // if the Thread exists, post an interrupt to it
            if (null != thread) {
                thread.interrupt();
            }
        }
    }
}

From source file:org.kududb.client.BaseKuduTest.java

@AfterClass
public static void tearDownAfterClass() throws Exception {
    try {/*from   w w w .j a  v  a 2  s.  c om*/
        if (client != null) {
            Deferred<ArrayList<Void>> d = client.shutdown();
            d.addErrback(defaultErrorCB);
            d.join(DEFAULT_SLEEP);
            // No need to explicitly shutdown the sync client,
            // shutting down the async client effectively does that.
        }
    } finally {
        if (startCluster) {
            for (Iterator<Process> masterIter = MASTERS.values().iterator(); masterIter.hasNext();) {
                masterIter.next().destroy();
                masterIter.remove();
            }
            for (Iterator<Process> tsIter = TABLET_SERVERS.values().iterator(); tsIter.hasNext();) {
                tsIter.next().destroy();
                tsIter.remove();
            }
            for (Thread thread : PROCESS_INPUT_PRINTERS) {
                thread.interrupt();
            }
        }
        for (String path : pathsToDelete) {
            try {
                File f = new File(path);
                if (f.isDirectory()) {
                    FileUtils.deleteDirectory(f);
                } else {
                    f.delete();
                }
            } catch (Exception e) {
                LOG.warn("Could not delete path {}", path, e);
            }
        }
    }
}