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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值