6

只需查看 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 上执行?

4

2 回答 2

11

撇开不说NEWTERMINATED“空闲”的意思是“等待东西”。这包括以下所有内容:

BLOCKED
WAITING
TIMED_WAITING

是否RUNNING只是不在 CPU 上执行?

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

于 2013-03-29T10:03:20.057 回答
3

A BLOCKED thread is one that has made a call to slow and/or shared resource. Since the thread cannot continue until the call returns, the thread is idle.

WAITING and TIMED_WAITING is when the thread is waiting for another thread (as opposed to some resource), and will be idle until the other thread allows it to resume.

NEW merely has not been scheduled on a CPU yet. It is essentially RUNNABLE, but this points out the fact that it was just created. I personally would not consider this idle.

RUNNABLE means it is either running, or is waiting to be assigned to a CPU.

于 2013-03-29T10:10:47.283 回答