whitehat
whitehat

Reputation: 2391

Does a thread of execution goes to "waiting" state by executing wait() and join() both?

There are 3 states for a thread that is alive but is neither running not runnable:-

  1. Sleeping
  2. Blocked
  3. Waiting

When a thread executes sleep() method, it goes into SLEEPING state from running state for the time period specified by its argument (say for some milliseconds).

When a thread is waiting for a lock on an object that is acquired by some other thread because of the synchronized method or block, it is BLOCKED by that thread.

So, can we say that a thread enters WAITING state when it executes wait() on some other thread?

Same is the case with calling join() on some thread.

So, can we say that both wait() (from java.lang.Object) and join() (from java.lang.Thread) shifts a thread's state to WAITING?

Upvotes: 2

Views: 2455

Answers (1)

JB Nizet
JB Nizet

Reputation: 691983

This is described in the javadoc of Thread.State:

public static final Thread.State WAITING

Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:

    Object.wait with no timeout
    Thread.join with no timeout
    LockSupport.park

A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

Upvotes: 5

Related Questions