Java 简明教程
Java - Thread Life Cycle
Life Cycle of a Thread in Java
Java 中线程的生命周期是指线程所经历的各种状态。例如,一个线程会诞生、启动、运行,然后死亡。 Thread class 定义了线程的生命周期和各种状态。
States of a Thread Life Cycle in Java
以下是生命周期的阶段 -
-
New - 新线程在其生命周期中处于新的状态。它一直保持此状态,直到程序启动线程。它也被称为 born thread。
-
Runnable - 新生的线程启动后,该线程变得可运行。处于此状态的线程被认为正在执行其任务。
-
Waiting - 有时,线程在等待另一个线程执行任务时会转变为等待状态。仅当其他线程发信号指示等待线程继续执行时,线程才会转换回可运行状态。
-
Timed Waiting - 可运行线程可以进入特定时间间隔的时间等待状态。当该时间间隔到期或它正在等待的事件发生时,处于此状态的线程会转换回可运行状态。
-
Terminated (Dead) - 当可运行线程完成其任务或以其他方式终止时,它会进入终止状态。
Java Example for Demonstrating Thread States
在该示例中,我们通过扩展 Thread 类来创建两个线程。我们正在打印线程的各个状态。当创建一个线程对象时,它的状态为 NEW;当调用 start() method 时,状态为 START;当调用 run() method 时,状态为 RUNNING;当一个线程完成处理 run() 方法时,它会转到 DEAD 状态。
package com.tutorialspoint;
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
ThreadDemo( String name) {
threadName = name;
System.out.println("Thread: " + threadName + ", " + "State: New");
}
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");
}
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-2, State: Running
Thread: Thread-2, 4
Thread: Thread-2, 3
Thread: Thread-2, 2
Thread: Thread-2, 1
Thread: Thread-2, State: Dead
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-1, 2
Thread: Thread-1, 1
Thread: Thread-1, State: Dead
More Examples on Thread Life Cycle & States
Example 1
在该示例中,我们正在使用 sleep() method 来引入处理的某些延迟,并且要演示使用线程的并行处理。我们正在通过扩展 Thread 类来创建两个线程。我们正在打印线程的各个状态。当创建一个线程对象时,它的状态为 NEW;当调用 start() 方法时,状态为 START;当调用 run() 方法时,状态为 RUNNING;如果调用了 sleep(),那么线程会转到 WAITING 状态;当一个线程完成处理 run() 方法时,它会转到 DEAD 状态。
package com.tutorialspoint;
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
ThreadDemo( String name) {
threadName = name;
System.out.println("Thread: " + threadName + ", " + "State: New");
}
public void run() {
System.out.println("Thread: " + threadName + ", " + "State: Running");
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
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
Example 2
在此示例中,我们通过实现 Runnable 类来创建两个线程。我们正在打印线程的每个状态。当创建线程对象时,它的状态为 NEW;当调用 start() 方法时,状态为 START;当调用 run() 方法时,状态为 RUNNING;如果调用了 sleep(),则线程将转到 WAITING 状态;当线程完成 run() 方法的处理时,它将转到 DEAD 状态。
package com.tutorialspoint;
class ThreadDemo implements Runnable {
private Thread t;
private String threadName;
ThreadDemo( String name) {
threadName = name;
System.out.println("Thread: " + threadName + ", " + "State: New");
}
public void run() {
System.out.println("Thread: " + threadName + ", " + "State: Running");
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
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