java condition await() 与object wait()的区别

java condition await() 与object wait()的区别

object wait()
* 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 {@code notify} method or the
* {@code notifyAll} method. The thread then waits until it can
* re-obtain ownership of the monitor and resumes execution.

object的方法关键点在于 当前线程和对象的监听器,调用此方法,会释放当前线程对于此对象的监听器,当前线程也处于等待 阻塞状态,前提是当前线程已经获取了CPU的的监听器,

condition await() 旨在当前线程的调度,与对象无关,调用此方法会是当前线程释放LOCK,并且当前线程会进行condition的等待队列,等待有其他condition调用dosignal()
会被唤醒,将当前线程重新放入同步队列中等待获取锁,

两种在表现上有点相似,但是作用机制是有区别的
1.object wait() 不能单独使用,必须是在synchronized 下才能使用,
2.object wait()必须要通过Nodify()方法进行唤醒
3.condition await() 必须是当前线程被排斥锁 lock 后,,获取到condition 后才能使用
4.condition await() 必须通过 sign() 方法进行唤醒

一个是基于对象监听器的同步方式,一个是基于 ASQ同步机制的同步方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值