What is the difference between blocked and waiting in Java thread state?

Source: Internet
Author: User

I was just looking at Csdn's question and answer. Find this problem.

The author of the original problem is the question raised when observing the output of the jstack. So what's the difference between blocked and waiting?
Replies can be found in the JDK source code, such as the following is part of the Java.lang.Thread.State class gaze.

/**
* thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state was waiting for A monitor lock
* To enter A synchronized Block/method or
* Reenter a Synchronized Block/method after calling
* {@link object#wait () object.wait}.
*/
BLOCKED,

/**
* thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* {@link object#wait () object.wait } with no timeout
* {@link #join () Thread.Join} with no timeout
* {@link locksupport#park () Locksupport.park}
**
* A thread in the waiting state was waiting for another thread to
* perform A particular action.
*
* For example, a thread of that have called  object.wait ()
* On an Object are waiting for another thread T o call
*  object.notify ()  or  object.notifyall ()  on
* this Object. A thread that had called  thread.join ()
* is waiting for a specified the thread to terminate.
*/
Waiting,

This gives a clear picture of where threads are in the blocked and waiting states.

Blocked status

The scenario in which the thread is in the blocked state.

    • The current thread is waiting for a monitor lock, for example, to wait for the synchronized code block to run or to use the synchronized tag.

    • The wait method of the type object is called in the synchronized block, such as the following example
      Synchronized (This)
      {
      while (flag)
      {
      Obj.wait ();
      }
      Some other code
      }
Waiting status

The scenario in which the thread is in the waiting state.

    • Calls the wait method of the object, but does not specify a time-out value.

    • The join method of the thread object is called, but the time-out value is not specified.

    • Call the park method of the Locksupport object.

Mention the waiting state, by the way, the scene of the timed_waiting state.

Timed_waiting status

The scenario in which the thread is in the timed_waiting state.

    • Call the Thread.Sleep method.
    • Call the wait method of the object to specify a time-out value.

    • Call the Join method of the Thread object to specify a time-out value.
    • Invokes the Parknanos method of the Locksupport object.
    • Invokes the Parkuntil method of the Locksupport object.
Welcome to Jackie's home. http://jackieathome.sinaapp.com/, if you need to reprint the article, please indicate the source.

What is the difference between blocked and waiting in Java thread state?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.