生命周期图

线程的 7 种状态
JDK 中用 Thread.State 枚举类定义了线程的几种状态,想要实现多线程,必须在主线程中创建新的线程对象。
Java 语言使用 Thread 及其子类的对象来表示线程,在它的一个完整的生命周期中通常要经历如下的 5 种状态。
NEW
1)新建
当一个Thread类或其子类的对象被声明并创建时,此时线程尚未启动,新生的线程对象处于新建状态。
RUNNABLE
2)就绪 ready
处于新建状态的线程被 start() 后,将进入线程队列等待 CPU 时间片,此时它已具备了运行的条件,只是没分配到 CPU 资源。
3)运行 running
当就绪的线程被调度并获得 CPU 资源时,便进入运行状态,run() 方法定义了线程的操作和功能。
BLOCKED
4)阻塞
进入一个同步块/方法前,处于阻塞状态的线程正在等待锁,或者在调用 Object.wait 后重新进入一个同步块/方法。阻塞时,线程让出 CPU 并临时终止自己的执行。
WAITING
5)等待中
一个等待中的线程的状态。一个线程由于调用了以下方法之一而处于等待状态。
- 没有设置超时时间的 Object.wait;
- 没有设置超时时间的 Thread.join;
- LockSupport.park
一个处于等待状态的线程正在等待另一个线程执行一个特定的动作。例如,一个在对象上调用了 Object.wait() 的线程正在等待另一个线程对该对象调用 Object.notify() 或 Object.notifyAll() 。一个调用了 Thread.join() 的线程正在等待一个指定的线程终止。
TIMED_WAITING**
6)定时等待
一个具有指定等待时间的等待线程的线程状态。一个线程由于以指定的正数等待时间调用了以下方法之一而处于定时的等待状态。
- Thread.sleep
- Object.wait with timeout
- Thread.join with timeout
- LockSupport.parkNanos
- LockSupport.parkUntil
TERMINATED
7)终止
线程完成了它的全部工作或线程被提前强制性地终止或出现异常导致结束
Thread.State 源码
public enum State {/*** Thread state for a thread which has not yet started.*/NEW,/*** Thread state for a runnable thread. A thread in the runnable* state is executing in the Java virtual machine but it may* be waiting for other resources from the operating system* such as processor.*/RUNNABLE,/*** Thread state for a thread blocked waiting for a monitor lock.* A thread in the blocked state is waiting for a monitor lock* to enter a synchronized block/method or* reenter a synchronized block/method after calling* {@link Object#wait() Object.wait}.*/BLOCKED,/*** Thread state for a waiting thread.* A thread is in the waiting state due to calling one of the* following methods:* <ul>* <li>{@link Object#wait() Object.wait} with no timeout</li>* <li>{@link #join() Thread.join} with no timeout</li>* <li>{@link LockSupport#park() LockSupport.park}</li>* </ul>** <p>A thread in the waiting state is waiting for another thread to* perform a particular action.** For example, a thread that has called {@code Object.wait()}* on an object is waiting for another thread to call* {@code Object.notify()} or {@code Object.notifyAll()} on* that object. A thread that has called {@code Thread.join()}* is waiting for a specified thread to terminate.*/WAITING,/*** Thread state for a waiting thread with a specified waiting time.* A thread is in the timed waiting state due to calling one of* the following methods with a specified positive waiting time:* <ul>* <li>{@link #sleep Thread.sleep}</li>* <li>{@link Object#wait(long) Object.wait} with timeout</li>* <li>{@link #join(long) Thread.join} with timeout</li>* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>* </ul>*/TIMED_WAITING,/*** Thread state for a terminated thread.* The thread has completed execution.*/TERMINATED;}
查看线程当前状态
public class Main {public static void main(String[] args) {Task task = new Task();task.setName("铸剑计划");System.out.println(task.getState());task.start();System.out.println(task.getState());while (task.getState() != Thread.State.TERMINATED){System.out.println(task.getState());try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(task.getState());}}class Task extends Thread {@Overridepublic void run() {for (int i = 0; i < 3; i++) {try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(getName() + ": " + i);}}}
