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

1、线程在Java中,任何时间点都存在以下任何一种状态

  1. New
  2. Runnable
  3. Blocked
  4. Waiting
  5. Timed Waiting
  6. Terminated

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


2、线程的生命周期

  1. New(新线程):创建新线程时,它处于新状态。当线程处于此状态时,线程尚未开始运行。当一个线程处于新状态时,它的代码还没有运行,也没有开始执行。
  2. Runnable(可运行状态):准备运行的线程被移到可运行状态。在这种状态下,一个线程可能实际上正在运行,也可能随时准备运行。线程调度程序负责为线程提供运行时间。
    多线程程序为每个线程分配固定的时间量。每个线程都会运行一段时间,然后暂停并将CPU交给另一个线程,这样其他线程就有机会运行了。当这种情况发生时,所有准备好运行、等待CPU和当前正在运行的线程都处于可运行状态。
         3. Blocked/Waiting(阻塞/等待状态):
      当线程暂时处于非活动状态时,它将处于以下状态之一:
      1. Blocked
      2. Waiting

例如,当线程等待I/O完成时,它处于阻塞状态。线程调度程序负责重新激活和调度阻塞/等待的线程。处于此状态的线程在移到可运行状态之前无法继续执行。处于这些状态的任何线程都不会占用任何CPU周期。

 当线程试图访问当前被其他线程锁定的受保护代码部分时,该线程处于阻塞状态。当受保护的部分被解锁时,调度将选择该部分被阻塞的线程之一,并将其移动到可运行状态。当线程处于等待状态时,它将等待另一个线程。当满足此条件时,调度程序将得到通知,等待的线程将被移动到可运行状态。

如果当前正在运行的线程被移到blocked/waiting状态,则线程调度程序会安排另一个处于runnable状态的线程运行。线程调度程序负责确定要运行哪个线程。

    4.Timed Waiting(定时等待):当线程调用带有超时参数的方法时,它处于定时等待状态。线程处于这种状态,直到超时完成或收到通知为止。例如,当一个线 程调用sleep或conditional wait时,它将被移到一个timed waiting状态。

    5.Terminated(终止状态):线程因以下原因之一终止:

 

    • 因为它正常存在。当线程的代码完全由程序执行时,就会发生这种情况。
    • 因为发生了一些异常的错误事件,如分段错误或未处理的异常。

处于终止状态的线程不再消耗任何CPU周期。

 

3、用Java实现线程状态

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

1、常量类型:New

声明:public static final Thread.State NEW

说明尚未启动的线程的线程状态。

2、常量类型:Runnable

声明:public static final Thread.State RUNNABLE

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

3、常量类型:Blocked

声明:public static final Thread.State BLOCKED

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

4、常量类型:Waiting

声明:public static final Thread.State WAITING

说明:由于调用以下方法之一使线程处于等待状态:

Object.wait 不带时间

Thread.join 不带时间

LockSupport.park

处于等待状态的线程正在等待另一个线程执行特定操作。

5、常量类型:Timed Waiting

声明:public static final Thread.State TIMED_WAITING

说明:具有指定等待时间的等待线程的线程状态。调用以下方法之一并且指定正等待时间让线程处于定时等待状态:

Thread.sleep 带时间

Object.wait 带时间

Thread.join 带时间

LockSupport.parkNanos

LockSupport.parkUntil

6、常量类型:Terminated

声明:public static final Thread.State TERMINATED

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

复制代码
// 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

说明:创建新线程时,该线程处于新状态。当对线程调用.start()方法时,线程调度程序会将其移到可运行状态。每当对线程实例调用join()方法时,执行该语句的当前线程将等待该线程移动到终止状态。因此,在控制台上打印最后一条语句之前,程序在thread2上调用join(),使thread1等待thread2完成执行并移动到Terminated状态。thread1进入Waiting状态,因为它正在等待thread2完成它的执行,因为它在thread2上调用了join。

posted @   我要去巴萨  阅读(333)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示