Example usage for java.lang InterruptedException InterruptedException

List of usage examples for java.lang InterruptedException InterruptedException

Introduction

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

Prototype

public InterruptedException(String s) 

Source Link

Document

Constructs an InterruptedException with the specified detail message.

Usage

From source file:Main.java

public static void exitOnTimeout() {
    if (Thread.interrupted()) {
        throw new RuntimeException(new InterruptedException("Exiting after timeout!"));
    }//from   ww w  . ja va  2 s  . c  o  m
}

From source file:Main.java

public static Dialog waitForDialogNotShowing(final Dialog dialog, final long timeout)
        throws InterruptedException {
    long start = System.currentTimeMillis();
    while (dialog.isShowing()) {
        Thread.sleep(50);//from  w  w  w . j av a  2 s  .  co m
        long now = System.currentTimeMillis();
        if (now - start > timeout) {
            throw new InterruptedException(
                    String.format("Timeout exceeded while waiting for dialog not showing"));
        }
    }
    return dialog;
}

From source file:Main.java

public static void waitForItemCount(final ListView listView, final int expectedCount, final long timeout)
        throws InterruptedException {
    long start = System.currentTimeMillis();
    while (listView.getAdapter().getCount() != expectedCount) {
        Thread.sleep(50);/*www.  ja v  a 2s  .co  m*/
        long now = System.currentTimeMillis();
        if (now - start > timeout) {
            throw new InterruptedException(
                    String.format("Timeout exceeded while waiting for an item count of %s", expectedCount));
        }
    }
}

From source file:Main.java

public static void waitForHitCount(final ActivityMonitor monitor, final int expectedCount, final long timeout)
        throws InterruptedException {
    long start = System.currentTimeMillis();
    while (monitor.getHits() != expectedCount) {
        Thread.sleep(50);/*  w  w  w. ja  v a2 s  .  c  o  m*/
        long now = System.currentTimeMillis();
        if (now - start > timeout) {
            throw new InterruptedException(
                    String.format("Timeout exceeded while waiting for a hit count of %s", expectedCount));
        }
    }
}

From source file:Main.java

public static void waitForStaticFieldNull(@SuppressWarnings("rawtypes") final Class clazz, final String name,
        final long timeout) throws Exception {
    long start = System.currentTimeMillis();
    while (!(getStaticFieldValue(clazz, name) == null)) {
        Thread.sleep(50);//  w  w w . ja  v  a  2  s  .c o  m
        long now = System.currentTimeMillis();
        if (now - start > timeout) {
            throw new InterruptedException(
                    String.format("Timeout exceeded while waiting for field %s to become null", name));
        }
    }
}

From source file:varioustests.swingworkertests.SearchForWordWorker.java

private static void failIfInterrupted() throws InterruptedException {
    if (Thread.currentThread().isInterrupted()) {
        throw new InterruptedException("Interrupted while searching files");
    }/*from www .j  a  va  2 s.  com*/
}

From source file:org.marketcetera.util.except.ExceptUtils.java

/**
 * Checks whether the calling thread has been interrupted, and, if
 * so, throws an {@link InterruptedException} with the default
 * interruption message and no underlying cause. The interrupted
 * status of the thread is cleared./*  w  w  w .j  a va  2s.c o m*/
 *
 * @throws InterruptedException Thrown if the calling thread
 * was interrupted.
 */

public static void checkInterruption() throws InterruptedException {
    if (Thread.currentThread().isInterrupted()) {
        throw new InterruptedException(Messages.THREAD_INTERRUPTED.getText());
    }
}

From source file:Main.java

public static Dialog waitForCurrentDialogShowing(final Activity activity, final long timeout)
        throws InterruptedException {
    long start = System.currentTimeMillis();
    try {//from w w  w  . ja v a2  s .c o  m
        Field currentDialogField = activity.getClass().getDeclaredField("currentDialog");
        currentDialogField.setAccessible(true);
        Dialog dialog = null;
        while (dialog == null || !dialog.isShowing()) {
            dialog = (Dialog) currentDialogField.get(activity);
            Thread.sleep(50);
            long now = System.currentTimeMillis();
            if (now - start > timeout) {
                throw new InterruptedException(
                        String.format("Timeout exceeded while waiting for dialog showing"));
            }
        }
        return dialog;
    } catch (Exception e) {
        throw new InterruptedException(String.format("Exception while waiting for dialog showing"));
    }
}

From source file:Main.java

public static void shutdownAndAwaitTermination(ExecutorService pool) {
    if (pool != null) {
        pool.shutdown(); // Disable new tasks from being submitted
        try {/*from   w  ww .  j av a  2  s . c om*/
            // Wait a while for existing tasks to terminate
            if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                pool.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                    throw new InterruptedException("Pool did not terminate");
                }
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            pool.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }
}

From source file:org.marketcetera.util.except.ExceptUtils.java

/**
 * Checks whether the calling thread has been interrupted, and, if
 * so, throws an {@link InterruptedException} with the given
 * interruption message, but without an underlying cause. The
 * interrupted status of the thread is cleared.
 *
 * @param message The message.//from   w  w  w  .  j  av a2 s  . c o m
 *
 * @throws InterruptedException Thrown if the calling thread
 * was interrupted.
 */

public static void checkInterruption(String message) throws InterruptedException {
    if (Thread.currentThread().isInterrupted()) {
        throw new InterruptedException(message);
    }
}