@Override public boolean isAlive() { return t.isAlive(); } }
/** * Indicates whether the server is currently running. * * @return True if the server is running, false otherwise. * * @see #start() * @see #stop() see WindowManagerService#isViewServerRunning() */ public boolean isRunning() { return mThread != null && mThread.isAlive(); }
private boolean taskObserverAlive() { return taskObserver != null && taskObserver.isAlive(); } }
@Override public boolean isAlive() { return t.isAlive(); } }
private void throwIfProducerClosed() { if (ioThread == null || !ioThread.isAlive()) throw new IllegalStateException("Cannot perform operation after producer has been closed"); }
@Override public boolean satisfy(Thread thread) { return !thread.isAlive() || thread.isInterrupted() || thread.isDaemon(); } });
@Override public void close() { if ((t != null) && (t.isAlive())) { t.interrupt(); try { t.join(); } catch (InterruptedException e) { log.warn("[Principal={}]: Error while waiting for Login thread to shutdown.", principal, e); Thread.currentThread().interrupt(); } } }
/** * Waits until the worker thread of this executor has no tasks left in its task queue and terminates itself. * Because a new worker thread will be started again when a new task is submitted, this operation is only useful * when you want to ensure that the worker thread is terminated <strong>after</strong> your application is shut * down and there's no chance of submitting a new task afterwards. * * @return {@code true} if and only if the worker thread has been terminated */ public boolean awaitInactivity(long timeout, TimeUnit unit) throws InterruptedException { if (unit == null) { throw new NullPointerException("unit"); } final Thread thread = this.thread; if (thread == null) { throw new IllegalStateException("thread was not started"); } thread.join(unit.toMillis(timeout)); return !thread.isAlive(); }
public void close() { if (refresherThread != null && refresherThread.isAlive()) { refresherThread.interrupt(); try { refresherThread.join(); } catch (InterruptedException e) { log.warn("[Principal={}]: Interrupted while waiting for re-login thread to shutdown.", principalLogText(), e); Thread.currentThread().interrupt(); } } }
private void notifyWatchees() { List<Entry> watchees = this.watchees; for (int i = 0; i < watchees.size();) { Entry e = watchees.get(i); if (!e.thread.isAlive()) { watchees.remove(i); try { e.task.run(); } catch (Throwable t) { logger.warn("Thread death watcher task raised an exception:", t); } } else { i ++; } } } }
@Override public void tearDown() throws Exception { interruptingTask.stopInterrupting(); interruptingThread.interrupt(); joinUninterruptibly(interruptingThread, 2500, MILLISECONDS); Thread.interrupted(); if (interruptingThread.isAlive()) { // This will be hidden by test-output redirection: logger.severe("InterruptenatorTask did not exit; future tests may be affected"); /* * This won't do any good under JUnit 3, but I'll leave it around in * case we ever switch to JUnit 4: */ fail(); } } });
/** * Spin-waits up to the specified number of milliseconds for the given thread to enter a wait * state: BLOCKED, WAITING, or TIMED_WAITING. */ void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { long startTime = System.nanoTime(); for (; ; ) { Thread.State s = thread.getState(); if (s == Thread.State.BLOCKED || s == Thread.State.WAITING || s == Thread.State.TIMED_WAITING) return; else if (s == Thread.State.TERMINATED) fail("Unexpected thread termination"); else if (millisElapsedSince(startTime) > timeoutMillis) { threadAssertTrue(thread.isAlive()); return; } Thread.yield(); } }
/** Checks that thread does not terminate within the given millisecond delay. */ void assertThreadStaysAlive(Thread thread, long millis) { try { // No need to optimize the failing case via Thread.join. delay(millis); assertTrue(thread.isAlive()); } catch (InterruptedException ie) { fail("Unexpected InterruptedException"); } }
/** Checks that the threads do not terminate within the given millisecond delay. */ void assertThreadsStayAlive(long millis, Thread... threads) { try { // No need to optimize the failing case via Thread.join. delay(millis); for (Thread thread : threads) assertTrue(thread.isAlive()); } catch (InterruptedException ie) { fail("Unexpected InterruptedException"); } }
public void testJoinWithNoWait() throws InterruptedException { Stopwatch stopwatch = Stopwatch.createStarted(); Thread thread = new Thread(new JoinTarget(15)); thread.start(); thread.join(); assertFalse(thread.isAlive()); joinUninterruptibly(thread); joinUninterruptibly(thread, 0, MILLISECONDS); joinUninterruptibly(thread, -42, MILLISECONDS); joinUninterruptibly(thread, LONG_DELAY_MS, MILLISECONDS); assertTimeNotPassed(stopwatch, LONG_DELAY_MS); }
public void testListenerDoesntDeadlockOnStopAndWaitFromTerminated() throws Exception { final NoOpThreadedService service = new NoOpThreadedService(); service.addListener( new Listener() { @Override public void terminated(State from) { service.stopAsync().awaitTerminated(); } }, directExecutor()); service.startAsync().awaitRunning(); Thread thread = new Thread() { @Override public void run() { service.stopAsync().awaitTerminated(); } }; thread.start(); thread.join(LONG_TIMEOUT_MILLIS); assertFalse(thread + " is deadlocked", thread.isAlive()); }
/** * Test for a bug where threads weren't getting signaled when shutdown was called, only when tasks * completed. */ public void testDirectExecutorService_awaitTermination_missedSignal() { final ExecutorService service = MoreExecutors.newDirectExecutorService(); Thread waiter = new Thread() { @Override public void run() { try { service.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) { return; } } }; waiter.start(); awaitTimedWaiting(waiter); service.shutdown(); Uninterruptibles.joinUninterruptibly(waiter, 10, TimeUnit.SECONDS); if (waiter.isAlive()) { waiter.interrupt(); fail("awaitTermination failed to trigger after shutdown()"); } }
public void testAddListenerAfterFailureDoesntCauseDeadlock() throws InterruptedException { final StartFailingService service = new StartFailingService(); service.startAsync(); assertEquals(State.FAILED, service.state()); service.addListener(new RecordingListener(service), directExecutor()); Thread thread = new Thread() { @Override public void run() { // Internally stopAsync() grabs a lock, this could be any such method on // AbstractService. service.stopAsync(); } }; thread.start(); thread.join(LONG_TIMEOUT_MILLIS); assertFalse(thread + " is deadlocked", thread.isAlive()); }
public void testAwaitTerminated() throws Exception { final NoOpService service = new NoOpService(); Thread waiter = new Thread() { @Override public void run() { service.awaitTerminated(); } }; waiter.start(); service.startAsync().awaitRunning(); assertEquals(State.RUNNING, service.state()); service.stopAsync(); waiter.join(LONG_TIMEOUT_MILLIS); // ensure that the await in the other thread is triggered assertFalse(waiter.isAlive()); }
public void testAwaitTerminated_FailedService() throws Exception { final ManualSwitchedService service = new ManualSwitchedService(); final AtomicReference<Throwable> exception = Atomics.newReference(); Thread waiter = new Thread() { @Override public void run() { try { service.awaitTerminated(); fail("Expected an IllegalStateException"); } catch (Throwable t) { exception.set(t); } } }; waiter.start(); service.startAsync(); service.notifyStarted(); assertEquals(State.RUNNING, service.state()); service.notifyFailed(EXCEPTION); assertEquals(State.FAILED, service.state()); waiter.join(LONG_TIMEOUT_MILLIS); assertFalse(waiter.isAlive()); assertThat(exception.get()).isInstanceOf(IllegalStateException.class); assertThat(exception.get()).hasCauseThat().isEqualTo(EXCEPTION); }