📜  Java中线程的生命周期和状态

📅  最后修改于: 2022-05-13 01:55:34.292000             🧑  作者: Mango

Java中线程的生命周期和状态

Java中的线程在任何时间点都存在以下任何一种状态。线程在任何时刻仅处于所示状态之一:

  1. 新的
  2. 可运行
  3. 被封锁
  4. 等待
  5. 定时等待
  6. 终止

下图表示了线程在任何时刻的各种状态。

线程的生命周期

  1. 新线程:当一个新线程被创建时,它处于新状态。当线程处于此状态时,线程尚未开始运行。当一个线程处于新状态时,它的代码还没有运行,也没有开始执行。
  2. 可运行状态准备运行的线程被移动到可运行状态。在这种状态下,线程可能实际上正在运行,或者它可能随时准备好运行。线程调度程序负责给线程运行时间。
    多线程程序为每个单独的线程分配固定的时间。每个线程运行一小段时间,然后暂停并将 CPU 让给另一个线程,以便其他线程有机会运行。当这种情况发生时,所有这些准备运行、等待 CPU 的线程和当前正在运行的线程都处于可运行状态。
  3. 阻塞/等待状态:当一个线程暂时处于非活动状态时,它处于以下状态之一:
    • 被封锁
    • 等待
  4. 定时等待:线程在调用带有超时参数的方法时处于定时等待状态。线程处于此状态,直到超时完成或收到通知。例如,当一个线程调用睡眠或条件等待时,它会进入定时等待状态。
  5. 终止状态:线程由于以下任一原因而终止:
    • 因为它正常退出。当线程的代码完全被程序执行时,就会发生这种情况。
    • 因为发生了一些不寻常的错误事件,例如分段错误或未处理的异常。

在Java中实现线程状态

在Java中,要获取线程的当前状态,请使用Thread.getState()方法获取线程的当前状态。 Java提供了Java.lang.Thread.State类,该类定义了线程状态的 ENUM 常量,如下所示:

1.新

Declaration: public static final Thread.State NEW

描述:尚未启动的线程的线程状态。

2. 可运行

Declaration: public static final Thread.State RUNNABLE

描述:可运行线程的线程状态。处于可运行状态的线程正在Java虚拟机中执行,但它可能正在等待来自操作系统的其他资源,例如处理器。

3. 被封锁

Declaration: public static final Thread.State BLOCKED

说明:线程阻塞等待监视器锁的线程状态。处于阻塞状态的线程正在等待监视器锁进入同步块/方法或调用 Object.wait() 后重新进入同步块/方法。

4. 等待

Declaration: public static final Thread.State WAITING

描述:等待线程的线程状态。等待线程的线程状态。由于调用以下方法之一,线程处于等待状态:

  • Object.wait 没有超时
  • 没有超时的 Thread.join
  • LockSupport.park

5. 定时等待

Declaration: public static final Thread.State TIMED_WAITING

描述:具有指定等待时间的等待线程的线程状态。由于以指定的正等待时间调用以下方法之一,线程处于定时等待状态:

  • 线程.sleep
  • Object.wait 超时
  • Thread.join 超时
  • LockSupport.parkNanos
  • LockSupport.parkUntil

6. 终止

Declaration: public static final Thread.State TERMINATED

说明:已终止线程的线程状态。线程已完成执行。

Java
// Java program to demonstrate thread states
class thread implements Runnable {
    public void run()
    {
        // moving thread2 to timed waiting state
        try {
            Thread.sleep(1500);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        System.out.println(
            "State of thread1 while it called join() method on thread2 -"
            + Test.thread1.getState());
        try {
            Thread.sleep(200);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
public class Test implements Runnable {
    public static Thread thread1;
    public static Test obj;
 
    public static void main(String[] args)
    {
        obj = new Test();
        thread1 = new Thread(obj);
 
        // thread1 created and is currently in the NEW
        // state.
        System.out.println(
            "State of thread1 after creating it - "
            + thread1.getState());
        thread1.start();
 
        // thread1 moved to Runnable state
        System.out.println(
            "State of thread1 after calling .start() method on it - "
            + thread1.getState());
    }
 
    public void run()
    {
        thread myThread = new thread();
        Thread thread2 = new Thread(myThread);
 
        // thread1 created and is currently in the NEW
        // state.
        System.out.println(
            "State of thread2 after creating it - "
            + thread2.getState());
        thread2.start();
 
        // thread2 moved to Runnable state
        System.out.println(
            "State of thread2 after calling .start() method on it - "
            + thread2.getState());
 
        // moving thread1 to timed waiting state
        try {
            // moving thread1 to timed waiting state
            Thread.sleep(200);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(
            "State of thread2 after calling .sleep() method on it - "
            + thread2.getState());
 
        try {
            // waiting for thread2 to die
            thread2.join();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(
            "State of thread2 when it has finished it's execution - "
            + thread2.getState());
    }
}


输出
State of thread1 after creating it - NEW
State of thread1 after calling .start() method on it - RUNNABLE
State of thread2 after creating it - NEW
State of thread2 after calling .start() method on it - RUNNABLE
State of thread2 after calling .sleep() method on it - TIMED_WAITING
State of thread1 while it called join() method on thread2 -WAITING
State of thread2 when it has finished it's execution - TERMINATED

解释:当一个新线程被创建时,该线程处于 NEW 状态。当在线程上调用 start() 方法时,线程调度程序将其移至 Runnable 状态。每当在线程实例上调用 join() 方法时,执行该语句的当前线程将等待该线程进入 Terminated 状态。因此,在控制台上打印最终语句之前,程序在 thread2 上调用 join(),使 thread1 等待,而 thread2 完成执行并进入 Terminated 状态。 thread1 进入 Waiting 状态,因为它正在等待 thread2 完成其执行,因为它已在 thread2 上调用 join。