Java Thread isAlive() Method
Example
Check if a thread is alive before it starts, while it runs, and after it is finished:
public class Main {
static class MyThread extends Thread {
public void run() {
System.out.println("While running: " + isAlive());
}
}
public static void main(String[] args) throws InterruptedException {
MyThread thread = new MyThread();
System.out.println("Before start: " + thread.isAlive());
thread.start();
thread.join();
System.out.println("After join: " + thread.isAlive());
}
}
Definition and Usage
The isAlive() method checks if a thread is alive. A thread is alive after it has started, and until it is finished.
In the example, join() waits for the thread to finish, so the output is always in the same order.
Syntax
public final boolean isAlive()
Parameter Values
None.
Technical Details
| Returns: | true if the thread is alive, otherwise false. |
|---|
Related Pages
❮ Thread Methods