JAVA的线程状态

  • JAVA 线程的状态

JAVA线程的状态

(首先说一下,我看网上有很多帖子说java线程有5种状态,实际上是不正确的。这里请各位客官不要把JAVA 线程的状态和操作系统线程的状态弄混淆了,JAVA线程的状态在JAVA的官方文档上已经明确表明了,有6种)

JAVA官方文档:

A thread state. A thread can be in one of the following states:

  • NEW
    A thread that has not yet started is in this state.
  • RUNNABLE
    A thread executing in the Java virtual machine is in this state.
  • BLOCKED
    A thread that is blocked waiting for a monitor lock is in this state.
  • WAITING
    A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
  • TIMED_WAITING
    A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
  • TERMINATED
    A thread that has exited is in this state.

A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.

线程状态,线程可以处以下线状态之一

  • 新建
    尚未启动的线程处以该状态。
  • 可运行
    在JAVA虚拟机中运行的线程处于此状态。
  • 阻塞
    等待监视器锁而被阻塞的线程处于此状态。
  • 等待
    无限期的等待另一个线程处理特定操作的线程处于此状态
  • 定时等待
    一个正在等待另一个线程处理特定操作的线程,但最多等待到指定的等待时间为止,该线程处于此状态。
  • 结束
    退出的线程处于此状态。

在给定的时间内,一个线程只能处于一个状态,这些状态是虚拟机状态。不反应任何操作系统线程状态。

  • NEW

    public static final Thread.State NEW

    Thread state for a thread which has not yet started.

  • RUNNABLE

    public static final Thread.State RUNNABLE

    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.

  • BLOCKED

    public static final Thread.State BLOCKED

    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 Object.wait.

  • WAITING

    public static final Thread.State WAITING

    Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:

    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.

  • TIMED_WAITING

    public static final Thread.State TIMED_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:
  • TERMINATED

    public static final Thread.State TERMINATED

    Thread state for a terminated thread. The thread has completed execution.

  • 新建

    public static final Thread.State NEW

    尚未启动的线程处于此状态

  • 可运行

    public static final Thread.State RUNNABLE

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

  • 阻塞

    public static final Thread.State BLOCKED

    线程的线程状态被阻塞,等待监视器锁。 处于阻塞状态的线程在调用Object.wait之后正在等待监视器锁,准备进入synchronized块/方法或再次进入synchronized块/方法。 

  • 等待

    public static final Thread.State WAITING

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

    处于等待状态的线程正在等待另一个线程执行特定操作。 例如:一个在Object上调用过 Object.wait() 的线程正在等待另一个线程在该对象上调用 Object.notify() 或者 Object.notifyAll()  。一个调用过 Thread.join()  的线程正在等待指定的线程终止。

  • 定时等待

    public static final Thread.State TIMED_WAITING

    具有指定等待时间的等待线程的线程状态。处于定时等待状态的线程,由于调用了以下方法之一,因此处于定时等待的状态:
  • 结束

    public static final Thread.State TERMINATED

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

(上面是我小学英语的水平加上百度翻译的成果,可能不太对,欢迎各位客官提出问题)

JAVA线程状态的官方文档:https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Thread.State.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值