Java中的线程生命周期指的是一个线程经历的各种状态。例如,一个线程从诞生开始,被启动,运行,最后消亡。Thread
类定义了线程的生命周期及其各种状态。
Java线程生命周期流程图
下图展示了线程完整的生命周期:
New -> Runnable -> Blocked (Waiting/Timed Waiting) -> Runnable -> Terminated (Dead)
Java线程生命周期的状态
以下是线程生命周期的各个阶段:
新建(New)
当一个新线程被创建而尚未开始运行时,处于这个状态。也被称为出生状态。
可运行(Runnable)
在新创建的线程被启动后,它进入可运行状态。此时线程可以被调度器选中并执行其任务。
等待(Waiting)
有时,线程会转移到等待状态,等待另一个线程完成某个任务。只有当另一个线程发出信号时,等待中的线程才会回到可运行状态。
定时等待(Timed Waiting)
一个可运行的线程可能会进入定时等待状态一段时间。当这段时间结束或者等待的事件发生时,线程就会回到可运行状态。
终止(Terminated/Dead)
当线程完成其任务或因其他原因终止时,它就进入了终止状态。
Java示例:展示线程状态
在这个例子中,我们通过继承 Thread
类来创建两个线程,并打印出每个线程的状态。当线程对象被创建时,它的状态是 NEW
;当 start()
方法被调用时,状态变为 START
;当 run()
方法被执行时,状态为 RUNNING
;当线程完成 run()
方法的处理后,它将进入 DEAD
状态。
package com.tutorialspoint;
class ThreadDemo extends Thread {
private String threadName;
public ThreadDemo(String name) {
threadName = name;
System.out.println("Thread: " + threadName + ", State: New");
}
@Override
public void run() {
System.out.println("Thread: " + threadName + ", State: Running");
for (int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
}
System.out.println("Thread: " + threadName + ", State: Dead");
}
@Override
public void start() {
System.out.println("Thread: " + threadName + ", State: Start");
super.start();
}
}
public class TestThread {
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo("Thread-1");
ThreadDemo thread2 = new ThreadDemo("Thread-2");
thread1.start();
thread2.start();
}
}
输出
Thread: Thread-1, State: New
Thread: Thread-2, State: New
Thread: Thread-1, State: Start
Thread: Thread-2, State: Start
Thread: Thread-1, State: Running
Thread: Thread-2, State: Running
Thread: Thread-1, 4
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread: Thread-1, State: Dead
Thread: Thread-2, State: Dead
更多线程生命周期与状态的例子
示例 1
在这个例子中,我们使用 sleep()
方法来引入一些处理延迟,并展示使用线程进行的并行处理。我们通过继承 Thread
类来创建两个线程,并打印出每个线程的状态。当线程对象被创建时,它的状态是 NEW
;当 start()
方法被调用时,状态变为 START
;当 run()
方法被执行时,状态为 RUNNING
;如果调用了 sleep()
,那么线程进入 WAITING
状态;当线程完成 run()
方法的处理后,它将进入 DEAD
状态。
package com.tutorialspoint;
class ThreadDemo extends Thread {
private String threadName;
public ThreadDemo(String name) {
threadName = name;
System.out.println("Thread: " + threadName + ", State: New");
}
@Override
public void run() {
System.out.println("Thread: " + threadName + ", State: Running");
try {
for (int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
System.out.println("Thread: " + threadName + ", State: Waiting");
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread: " + threadName + ", State: Dead");
}
@Override
public void start() {
System.out.println("Thread: " + threadName + ", State: Start");
super.start();
}
}
public class TestThread {
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo("Thread-1");
ThreadDemo thread2 = new ThreadDemo("Thread-2");
thread1.start();
thread2.start();
}
}
输出
Thread: Thread-1, State: New
Thread: Thread-2, State: New
Thread: Thread-1, State: Start
Thread: Thread-2, State: Start
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, State: Waiting
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-2, State: Waiting
Thread: Thread-1, State: Waiting
Thread: Thread-2, 2
Thread: Thread-1, 2
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Waiting
Thread: Thread-2, 1
Thread: Thread-2, State: Waiting
Thread: Thread-1, 1
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Dead
Thread: Thread-1, State: Dead
示例 2
在这个例子中,我们通过实现 Runnable
接口来创建两个线程,并打印出每个线程的状态。当线程对象被创建时,它的状态是 NEW
;当 start()
方法被调用时,状态变为 START
;当 run()
方法被执行时,状态为 RUNNING
;如果调用了 sleep()
,那么线程进入 WAITING
状态;当线程完成 run()
方法的处理后,它将进入 DEAD
状态。
package com.tutorialspoint;
class ThreadDemo implements Runnable {
private Thread t;
private String threadName;
public ThreadDemo(String name) {
threadName = name;
System.out.println("Thread: " + threadName + ", State: New");
}
@Override
public void run() {
System.out.println("Thread: " + threadName + ", State: Running");
try {
for (int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
System.out.println("Thread: " + threadName + ", State: Waiting");
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread: " + threadName + ", State: Dead");
}
public void start() {
System.out.println("Thread: " + threadName + ", State: Start");
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}
public class TestThread {
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo("Thread-1");
ThreadDemo thread2 = new ThreadDemo("Thread-2");
thread1.start();
thread2.start();
}
}
输出
Thread: Thread-1, State: New
Thread: Thread-2, State: New
Thread: Thread-1, State: Start
Thread: Thread-2, State: Start
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, State: Waiting
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-2, State: Waiting
Thread: Thread-1, State: Waiting
Thread: Thread-2, 2
Thread: Thread-1, 2
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Waiting
Thread: Thread-2, 1
Thread: Thread-2, State: Waiting
Thread: Thread-1, 1
Thread: Thread-1, State: Waiting
Thread: Thread-2, State: Dead
Thread: Thread-1, State: Dead