Sorry for the not-very professional question but I am new in Java and thread programming concept. If thread is sleep or waitting, is it alive or not? What would return thread.isAlive() True ore False in these two cases?
Asked
Active
Viewed 113 times
-2
-
2I guess trying it and finding out was out of the question? Or even reading the docs? – Brian Roach Dec 28 '13 at 22:56
-
This is a lazy Question. In future, please find and read the available documentation BEFORE you post Questions like this. – Stephen C Dec 29 '13 at 00:36
-
Duplicate of http://stackoverflow.com/questions/17293304/when-is-a-java-thread-alive – Stephen C Dec 29 '13 at 00:38
2 Answers
3
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());
}
}

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
thread dies when it finish its work (little bit simplified answer and not perfectly correct but it most cases) – JosefN Dec 28 '13 at 22:55