本文整理了Java中java.lang.Thread.isAlive()
方法的一些代码示例,展示了Thread.isAlive()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.isAlive()
方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:isAlive
[英]Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
[中]测试此线程是否处于活动状态。如果线程已启动但尚未死亡,则该线程是活动的。
代码示例来源:origin: netty/netty
@Override
public boolean isAlive() {
return t.isAlive();
}
}
代码示例来源:origin: androidannotations/androidannotations
/**
* 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();
}
代码示例来源:origin: airbnb/lottie-android
private boolean taskObserverAlive() {
return taskObserver != null && taskObserver.isAlive();
}
}
代码示例来源:origin: redisson/redisson
@Override
public boolean isAlive() {
return t.isAlive();
}
}
代码示例来源:origin: apache/kafka
private void throwIfProducerClosed() {
if (ioThread == null || !ioThread.isAlive())
throw new IllegalStateException("Cannot perform operation after producer has been closed");
}
代码示例来源:origin: ctripcorp/apollo
@Override
public boolean satisfy(Thread thread) {
return !thread.isAlive() || thread.isInterrupted() || thread.isDaemon();
}
});
代码示例来源:origin: apache/kafka
@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();
}
}
}
代码示例来源:origin: netty/netty
/**
* 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();
}
代码示例来源:origin: apache/kafka
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();
}
}
}
代码示例来源:origin: netty/netty
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 ++;
}
}
}
}
代码示例来源:origin: google/guava
@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();
}
}
});
代码示例来源:origin: google/guava
/**
* 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();
}
}
代码示例来源:origin: google/guava
/** 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");
}
}
代码示例来源:origin: google/guava
/** 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");
}
}
代码示例来源:origin: google/guava
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);
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
/**
* 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()");
}
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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);
}
内容来源于网络,如有侵权,请联系作者删除!