Appearance
Thread States
In a Java program, a thread object can only call the start() method once to launch a new thread, which executes the run() method. Once the run() method completes, the thread terminates. Therefore, the states of a Java thread are as follows:
- New: A thread that has been created but has not yet started execution.
- Runnable: A thread that is currently executing the
run()method. - Blocked: A running thread that is suspended due to some blocking operation.
- Waiting: A running thread that is waiting for some operation to complete.
- Timed Waiting: A running thread that is waiting due to the execution of the
sleep()method with a specified timeout. - Terminated: A thread that has completed execution because the
run()method has finished.
The state transitions can be represented in the following state diagram:
┌─────────────┐
│ New │
└─────────────┘
│
▼
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
┌─────────────┐ ┌─────────────┐
││ Runnable │ │ Blocked ││
└─────────────┘ └─────────────┘
│┌─────────────┐ ┌─────────────┐│
│ Waiting │ │Timed Waiting│
│└─────────────┘ └─────────────┘│
─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
│
▼
┌─────────────┐
│ Terminated │
└─────────────┘After a thread starts, it can switch among the Runnable, Blocked, Waiting, and Timed Waiting states until it eventually reaches the Terminated state, at which point the thread ends.
Reasons for Thread Termination
A thread can terminate for the following reasons:
- Normal Termination: The
run()method reaches areturnstatement. - Unexpected Termination: The
run()method terminates due to an unhandled exception. - Forced Termination: The
stop()method is called on aThreadinstance to forcefully terminate it (strongly discouraged).
Waiting for Another Thread to Finish
A thread can wait for another thread to complete before continuing its execution. For example, after starting thread t, the main thread can wait for it to finish using t.join():
java
// Multithreading
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
System.out.println("Hello");
});
System.out.println("Start");
t.start(); // Start thread t
t.join(); // Main thread will wait for t to finish
System.out.println("End");
}
}When the main thread calls join() on the thread object t, it will wait until the thread represented by t completes. Therefore, the printed order is guaranteed to be: first "Start" from the main thread, then "Hello" from thread t, and finally "End" from the main thread.
If thread t has already finished, calling join() on it will return immediately. Additionally, the overloaded method join(long) allows you to specify a wait time; if the thread does not finish within that time, it will stop waiting.
Summary
- The states of a Java
Threadobject include:New,Runnable,Blocked,Waiting,Timed Waiting, andTerminated. - You can wait for another thread to finish by calling the
join()method on its thread object. - You can specify a wait time; if the time is exceeded and the thread has not finished, it will stop waiting.
- Calling
join()on a thread that has already completed will return immediately.