• 【Java】线程都有哪几种状态


    前言

    首先我们要知道,在传统(操作系统)的线程模型中线程被分为五种状态,在java线程中,线程被分为六种状态。

    传统线程模型(操作系统)中线程状态

    在这里插入图片描述

    线程的五种状态:

    1. 新建(new)

      创建了一个新的线程对象

    2. 就绪(runnable)

      调用线程的start()方法,处于就绪状态

    3. 运行(running)

      获得了CPU时间片,执行程序代码

      就绪状态是进入到运行状态的唯一入口

    4. 阻塞(block)

      因为某种原因,线程放弃对CPU的使用权,停止执行,直到进入就绪状态在有可能再次被CPU调度

      阻塞又分为三种:

      • 等待阻塞:运行状态的线程执行wait方法,JVM会把线程放在等待队列中,使本线程进入阻塞状态。

      • 同步阻塞:线程在获得synchronized同步锁失败,JVM会把线程放入锁池中,线程进入同步阻塞。对于锁池和等待池,可以看这篇文章

      • 其他阻塞:调用线程的sleep()或者join()后,线程会进入道阻塞状态,当sleep超时或者join终止或超时,线程重新转入就绪状态

    5. 死亡(dead)

      线程run()、main()方法执行结束,或者因为异常退出了run()方法,则该线程结束生命周期

      死亡的线程不可再次复生

    Java线程中的状态

    通过查看Thread类的State方法,我们可以看到Java线程其实是六种状态

    public enum State {
            /**
             * Thread state for a thread which has not yet started.
             */
            NEW,
     
            /**
             * Thread state for a runnable thread.  A thread in the runnable
             * state is executing in the Java virtual machine but it may
             * be waiting for other resources from the operating system
             * such as processor.
             */
            RUNNABLE,
     
            /**
             * Thread state for a thread blocked waiting for a monitor lock.
             * A thread in the blocked state is waiting for a monitor lock
             * to enter a synchronized block/method or
             * reenter a synchronized block/method after calling
             * {@link Object#wait() Object.wait}.
             */
            BLOCKED,
     
            /**
             * Thread state for a waiting thread.
             * A thread is in the waiting state due to calling one of the
             * following methods:
             * 
      *
    • {@link Object#wait() Object.wait} with no timeout
    • *
    • {@link #join() Thread.join} with no timeout
    • *
    • {@link LockSupport#park() LockSupport.park}
    • *
    * *

    A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * is waiting for a specified thread to terminate. */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: *

      *
    • {@link #sleep Thread.sleep}
    • *
    • {@link Object#wait(long) Object.wait} with timeout
    • *
    • {@link #join(long) Thread.join} with timeout
    • *
    • {@link LockSupport#parkNanos LockSupport.parkNanos}
    • *
    • {@link LockSupport#parkUntil LockSupport.parkUntil}
    • *
    */
    TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    我们可以看到线程实际上是分为六种状态的,既

    1. 初始状态(NEW)
      线程被构建,但是还没有调用start方法

    2. 运行状态(RUNNABLE)
      Java线程把操作系统中就绪和运行两种状态统一称为“运行中”

    3. 阻塞状态(BLOCKED)
      表示线程进入等待状态,也就是线程因为某种原因放弃了CPU的使用权,阻塞也分为几种情况(当一个线程试图获取一个内部的对象锁(非java.util.concurrent库中的锁),而该锁被其他线程持有,则该线程进入阻塞状态。)

      • 等待阻塞:运行的线程执行了Thread.sleep、wait、join等方法,JVM会把当前线程设置为等待状态,当sleep结束,join线程终止或者线程被唤醒后,该线程从等待状态进入阻塞状态,重新占用锁后进行线程恢复

      • 同步阻塞:运行的线程在获取对象的同步锁时,若该同步锁被其他线程锁占用了,那么JVM会把当前项城放入到锁池中

      • 其他阻塞:发出I/O请求,JVM会把当前线程设置为阻塞状态,当I/O处理完毕则线程恢复

    4. 等待(WAITING)
      等待状态,没有超时时间(无限等待),要被其他线程或者有其他的中断操作
      执行wait、join、LockSupport.park()

    5. 超时等待(TIME_WAITING)
      与等待不同的是,不是无限等待,超时后自动返回
      执行sleep,带参数的wait等可以实现

    6. 终止(Teminated)
      代表线程执行完毕

    线程的运行流程

    在这里插入图片描述

    线程首先被new创建,进入初始状态

    然后线程调用start方法,进入就绪状态

    这里要注意,线程只要抢占了cpu时间片,可以不用获取全部的锁就可以运行,但是当运行到需要的锁没有获得时,会进入阻塞状态

    当一个线程被sleep后,线程会先进入超时等待状态,当时间结束后,会先进入等待阻塞状态,当有锁以后再进入就绪状态

  • 相关阅读:
    Mysql主从复制与读写分离
    RabbitMQ如何确保消息发送和消息接收
    达梦数据库安装和使用
    CAD进阶练习(二)
    C2基础设施威胁情报对抗策略
    Python 中的 Elias Delta 编码
    Java—类加载机制
    聊聊绩效考核:松松网编辑跑路了吗?
    【Python基础篇015】第叁章模块大全之《 os模块》
    【Android Studio】Git可视化基本操作
  • 原文地址:https://blog.csdn.net/u011397981/article/details/132722036