java线程wait_Java线程中wait状态和block状态的区别?

Thread is in the Blocked state while waiting for the monitor lock to enter a synchronized block or method or to reenter a synchronized block or method after calling Object.wait().

这个地方是说synchronized会导致线程进入Blocked状态,Object.wait()导致线程进入Waiting状态,Waiting线程被其他线程调用Object.notify()唤醒之后,重新获取对象上的锁的时候也会进入Blocked状态

* The current thread must own this object's monitor. The thread

* releases ownership of this monitor and waits until another thread

* notifies threads waiting on this object's monitor to wake up

* either through a call to the {@codenotify} method or the

* {@codenotifyAll} method. The thread then waits until it can

* re-obtain ownership of the monitor and resumes execution.

这是Object.wait()方法的注释,最后一句指明被唤醒的线程需要重新获取到对象的锁才能恢复执行

* The awakened thread will not be able to proceed until the current

* thread relinquishes the lock on this object. The awakened thread will

* compete in the usual manner with any other threads that might be

* actively competing to synchronize on this object; for example, the

* awakened thread enjoys no reliable privilege or disadvantage in being

* the next thread to lock this object.

这是Object.notify()方法的注释,第一句也说被唤醒的线程需要获取对象的锁

也就是说只有synchronized会导致线程进入Blocked状态,Waiting状态只能进入Blocked状态,获取锁之后才能恢复执行。


版权声明:本文为weixin_39521835原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。