List of usage examples for java.lang Thread interrupted
public static boolean interrupted()
From source file:Main.java
public static void sleepNanos(long nanoDuration) throws InterruptedException { final long end = System.nanoTime() + nanoDuration; long timeLeft = nanoDuration; do {/*w w w . ja v a 2 s . c om*/ if (timeLeft > SLEEP_PRECISION) { Thread.sleep(1); } else if (timeLeft > SPIN_YIELD_PRECISION) { Thread.sleep(0); } timeLeft = end - System.nanoTime(); if (Thread.interrupted()) throw new InterruptedException(); } while (timeLeft > 0); }
From source file:Main.java
/** * Attempt to join the thread specified safely. * //from www . j ava 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:Main.java
public static void exitOnTimeout() { if (Thread.interrupted()) { throw new RuntimeException(new InterruptedException("Exiting after timeout!")); }/* w w w . ja va 2 s. co m*/ }
From source file:Main.java
private static void throwExceptionIfInterruptedOrCancelled(AtomicBoolean isCanceled) throws InterruptedException { if (Thread.interrupted() || (isCanceled != null && isCanceled.get())) { throw new InterruptedException(); }/* www . j a v a2 s. c o m*/ }
From source file:Test.java
private static void startUpdateThread(int i, final ConcurrentMap<Integer, String> concurrentMap) { Thread thread = new Thread(new Runnable() { public void run() { while (!Thread.interrupted()) { Random random = new Random(); int randomInt = random.nextInt(20); concurrentMap.put(randomInt, UUID.randomUUID().toString()); }/*from w w w . j a va2s.c o m*/ } }); thread.setName("Update Thread " + i); updateThreads.add(thread); thread.start(); }
From source file:Test.java
private static void startUpdatingThread(final List<String> list) { updatingThread = new Thread(new Runnable() { long counter = 0; public void run() { while (!Thread.interrupted()) { int size = list.size(); Random random = new Random(); if (random.nextBoolean()) { if (size > 1) { list.remove(random.nextInt(size - 1)); }/* w w w. j a v a2 s .com*/ } else { if (size < 20) { list.add("Random string " + counter); } } counter++; } } }); updatingThread.start(); }
From source file:MyThread.java
public void run() { try {/*from w ww. j av a2s . c o m*/ Thread.sleep(30000); for (int i = 1; i < 10; i++) { if (Thread.interrupted()) { System.out.println("interrupted."); break; } } } catch (Exception exc) { System.out.println(Thread.currentThread().getName() + " interrupted."); } System.out.println(Thread.currentThread().getName() + " exiting."); }
From source file:MyThread.java
public void run() { System.out.println(Thread.currentThread().getName() + " starting."); try {//ww w.j a va 2 s. c o m Thread.sleep(30000); for (int i = 1; i < 10; i++) { if (Thread.interrupted()) { System.out.println("interrupted."); break; } System.out.print("."); for (long x = 0; x < 1000; x++) System.out.println("/"); } } catch (Exception exc) { System.out.println(Thread.currentThread().getName() + " interrupted."); } System.out.println(Thread.currentThread().getName() + " exiting."); }
From source file:org.marketcetera.util.except.ExceptUtilsTest.java
private static void interruptHelper(Exception ex, boolean interrupted) { assertEquals(interrupted, ExceptUtils.interrupt(ex)); assertEquals(interrupted, Thread.interrupted()); }
From source file:Main.java
/** * <p>//from w ww.ja va 2 s. co m * Puts the current thread to sleep for the specified number of milliseconds. * </p> * <p> * If the thread was interrupted before the sleep method is called, the sleep method wont sleep. * </p> * @param milliseconds number of milliseconds to sleep. * * @return <code>true</code> if the thread slept uninterrupted for the specified milliseconds, <code>false</code> if * it was interrupted. */ public static boolean sleep(long milliseconds) { //checking if we got pre-interrupted. boolean interrupted = Thread.interrupted(); if (!interrupted) { try { Thread.sleep(milliseconds); } catch (InterruptedException ex) { interrupted = true; // clearing the interrupt flag Thread.interrupted(); } } return !interrupted; }