If thread is sleep or waitting, is it alive or not? What would return thread.isAlive() True ore False in these two cases?
It's still alive, just not running.
From the documentation:
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
A thread which is just sleeping or waiting hasn't died (it hasn't exited its run
method, either normally or abruptly), so isAlive
will return true.
Of course it's very easy to test this:
public class Test {
public static void main(String[] args) throws Exception {
Runnable runnable = new Runnable() {
@Override public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
};
Thread thread = new Thread(runnable);
thread.start();
// Give the new thread plenty of time to really start
Thread.sleep(5000);
System.out.println(thread.isAlive());
}
}
See more on this question at Stackoverflow