Java线程什么时候空闲?



看一下Java线程状态:

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.

为什么没有空闲状态?或者什么状态最接近于空闲线程?
RUNNING只是不在CPU上执行吗?

撇开NEWTERMINATED不谈,"idle"的意思是"等待东西"。这包括以下所有内容:

BLOCKED
WAITING
TIMED_WAITING

RUNNING,但只是不执行CPU?

没有RUNNING,只有RUNNABLE。这大致意味着"有事情要做",但没有说明线程现在是否正在运行(它可能正在等待一个核心可用)。

阻塞线程是调用慢速和/或共享资源的线程。因为在调用返回之前线程不能继续,所以线程是空闲的。

WAITING and time_waiting是指线程正在等待另一个线程(而不是某些资源),并且在另一个线程允许它恢复之前将处于空闲状态。

NEW只是还没有在CPU上调度。它本质上是RUNNABLE,但这指出了它刚刚被创建的事实。我个人不会认为这是徒劳的。

RUNNABLE表示正在运行,或者正在等待分配给CPU。

最新更新