多线程:6种状态及其转换条件

📅 2026/7/7 15:57:37 👁️ 阅读次数 📝 编程学习
多线程:6种状态及其转换条件

根据Thread类的源码,Java线程共有6种状态

一,NEW(新建状态)

线程被创建但尚未启动的状态。

当使用`new Thread()`创建一个线程对象后,但还没有调用`start()`方法时,线程就处于NEW状态。此时线程已经分配了内存空间,但还没有开始执行。

Thread thread = new Thread(() -> {
System.out.println("线程执行中");
});
System.out.println("线程状态:" + thread.getState()); // 输出:NEW

二,RUNNABLE(可运行状态)

线程正在Java虚拟机中执行或准备执行,等待系统资源(如CPU时间片)。

RUNNABLE状态实际上包含了两种子状态:
- Ready:线程已经准备好运行,正在等待CPU时间片
- Running:线程正在CPU上执行

需要注意的是,处于RUNNABLE状态的线程不一定正在执行,可能只是在等待CPU调度。

Thread thread = new Thread(() -> {
System.out.println("线程状态:" + Thread.currentThread().getState()); // 输出:RUNNABLE
});
thread.start();

三,BLOCKED(阻塞状态)

线程被阻塞,正在等待获取一个监视器锁(monitor lock)。

当线程试图进入一个被其他线程持有的同步代码块或方法时,就会进入BLOCKED状态。一旦锁被释放,等待的线程就有机会获取锁并继续执行。
public class BlockedExample {
private static final Object lock = new Object();

public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
synchronized (lock) {
try {
Thread.sleep(5000); // 持有锁5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

Thread thread2 = new Thread(() -> {
synchronized (lock) {
System.out.println("thread2获取到锁");
}
});

thread1.start();
try { Thread.sleep(100); } catch (InterruptedException e) {}
thread2.start();

try { Thread.sleep(100); } catch (InterruptedException e) {}
System.out.println("thread2状态:" + thread2.getState()); // 输出:BLOCKED
}
}

四,WAITING(无限期等待状态)

线程无限期地等待另一个线程执行特定操作。

进入WAITING状态的方法:
- `Object.wait()` - 不带超时时间
- `Thread.join()` - 不带超时时间
- `LockSupport.park()`

退出WAITING状态的条件:
- 其他线程调用对应对象的`notify()`或`notifyAll()`
- 其他线程中断当前线程
- 等待的线程执行完成(针对join情况)

public class WaitingExample {
public static void main(String[] args) throws InterruptedException {
Object lock = new Object();

Thread waitingThread = new Thread(() -> {
synchronized (lock) {
try {
lock.wait(); // 进入WAITING状态
System.out.println("被唤醒,继续执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

waitingThread.start();
Thread.sleep(100);
System.out.println("等待线程状态:" + waitingThread.getState()); // 输出:WAITING

synchronized (lock) {
lock.notify(); // 唤醒等待线程
}
}
}

五,TIMED_WAITING(限期等待状态)

线程在指定的时间范围内等待。

进入TIMED_WAITING状态的方法:
- `Thread.sleep(long millis)`
- `Object.wait(long timeout)`
- `Thread.join(long millis)`
- `LockSupport.parkNanos(long nanos)`
- `LockSupport.parkUntil(long deadline)`

public class TimedWaitingExample {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
try {
Thread.sleep(5000); // 睡眠5秒,进入TIMED_WAITING
} catch (InterruptedException e) {
e.printStackTrace();
}
});

thread.start();
Thread.sleep(100);
System.out.println("线程状态:" + thread.getState()); // 输出:TIMED_WAITING
}
}

六,TERMINATED(终止状态)

线程已经执行完毕或异常结束。

线程执行完`run()`方法或者因为未捕获的异常而退出时,就会进入TERMINATED状态。一旦进入此状态,线程就不能再次启动。

public class TerminatedExample {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
System.out.println("线程执行完成");
});

thread.start();
thread.join(); // 等待线程执行完成
System.out.println("线程状态:" + thread.getState()); // 输出:TERMINATED
}
}

七,线程状态转换图