Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a Java thread idle?

Just looking thru the 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.

How come there is no idle state? Or what state most closely represents an idle thread?
Is RUNNING but just not executing on CPU?

like image 906
More Than Five Avatar asked Mar 29 '13 10:03

More Than Five


Video Answer


2 Answers

Leaving NEW and TERMINATED aside, "idle" means "waiting for stuff". This includes all of the following:

BLOCKED
WAITING
TIMED_WAITING

Is RUNNING but just not executing on CPU?

There's noRUNNING, there's RUNNABLE. This roughly means "got stuff to do", but doesn't say anything about whether the thread is actually running right now (it might be waiting for a core to become available).

like image 115
NPE Avatar answered Sep 28 '22 09:09

NPE


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.

like image 26
CapnSmack Avatar answered Sep 28 '22 07:09

CapnSmack