什么时候Java线程空闲?

时间:2013-03-29 10:01:39

标签: java multithreading

只是看看Java thread 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.

怎么没有闲置状态?或者什么状态最接近代表空闲线程?
RUNNING但只是没有在CPU上执行吗?

2 个答案:

答案 0 :(得分:11)

NEWTERMINATED放在一边,“空闲”意味着“等待东西”。这包括以下所有内容:

BLOCKED
WAITING
TIMED_WAITING
  

RUNNING但是没有在CPU上执行吗?

没有RUNNING,有RUNNABLE。这大致意味着“有事要做”,但没有说明线程是否实际上正在运行(它可能正在等待核心变得可用)。

答案 1 :(得分:3)

BLOCKED线程是一个调用慢速和/或共享资源的线程。由于线程在调用返回之前无法继续,因此线程处于空闲状态。

WAITING和TIMED_WAITING是线程正在等待另一个线程(而不是某个资源),并且在其他线程允许其恢复之前将处于空闲状态。

NEW尚未在CPU上安排。它本质上是RUNNABLE,但这指出它刚刚创建的事实。我个人不会认为这是空闲的。

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