- 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:
Object.wait
with no timeoutThread.join
with no timeoutLockSupport.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 callObject.notify()
orObject.notifyAll()
on that object. A thread that has calledThread.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:
Thread.sleep
Object.wait
with timeoutThread.join
with timeoutLockSupport.parkNanos
LockSupport.parkUntil
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.wait
没有超时时间Thread.join
没有超时时间LockSupport.park
处于等待状态的线程正在等待另一个线程执行特定操作。 例如:一个在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