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

CSDN博客 · · 909 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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状态,获取锁之后才能恢复执行。

909 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传