Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What state is a sleeping thread in?

I'm looking for verification/arguments on the following:

A thread is in exactly one of the 5 (+1 -- 2 for WAITING) states at any point of time.

Suppose a thread T calls

Thread.sleep(3000);

and thus puts itself into sleep for 3 secs.

Which state is it in during those 3 secs?

It's clearly start()-ed and is still alive, thus is neither NEW nor TERMINATED.

It isn't waiting on any lock. in fact, it hasn't released any of the locks it had just before this call, so it isn't BLOCKED.

It's not waiting on another thread. Is waiting on itself in some sense-- but that's not the specification of the WAITING state. What gets a thread into a WAITING state is one of the calls: Object.wait(), Thread.join() and LockSupport.park().

So it should be RUNNABLE -- the only one left out among the thread states. However, RUNNABLE is the state a thread is executing on JVM-- is enabled for CPU time and thus for burning resources. A sleeping thread being on CPU schedule seems contradictory.

Am i missing anything here?

TIA.

like image 999
Roam Avatar asked Nov 16 '14 23:11

Roam


People also ask

How can I tell if a thread is sleeping?

You can call Thread. getState() on and check if the state is TIMED_WAITING . Note, however that TIMED_WAITING doesn't necessarily mean that the thread called sleep() , it could also be waiting in a Object. wait(long) call or something similar.

Is sleeping state of thread in Java?

sleep() sends the current thread into the “Not Runnable” state for some amount of time. The thread keeps the monitors it has acquired — i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t. interrupt() .

Why sleep of thread is static?

Why is sleep () a static method? This is because whenever you are calling these methods, those are applied on the same thread that is running. You can't tell another thread to perform some operation like, sleep() or wait .


1 Answers

The Javadoc of Thead.State.TIMED_WAITING reads:

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 timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil

And indeed, the following test program prints TIMED_WAITING:

public class Test {
    public static void main(String[] args) throws Exception {
        Thread t = new Thread() {
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
        };
        t.start();
        Thread.sleep(100); // make sure the other thread has started
        System.out.println(t.getState());
    }
}
like image 185
meriton Avatar answered Nov 14 '22 22:11

meriton