Search code examples
javaconcurrencylockingwaitnotify

Can I use ConditionObject instead of getting it from a lock?


Every source i saw in the www states that a Condition Object is get by using the newCondition method on a Lock object

ReentrantLock.newCondition()

But by navigating the java sources i saw that a implementation is already available.

ConditionObject // implements Condition

I dont want to be the only one but thats why I am asking.

Can I Use this Condition Object to wait/notify/notifyAll with synchronized methods?

Or is it any better to stick to the combination with locks?

Additionally:

There is so much code in the await() method of a Condition object. Could there by a performance difference between the traditional wait/notify/notifyAll?


Solution

  • I'm not sure where did you find that ConditionObject, but it's most likely some internal object used by a lock class.

    Condition cannot exist by itself without a lock (a monitor, to be precise), that's because await/signal need access to that monitor. When you call await method it'll put current thread on the wait list of the monitor, suspends it (parks it, so it's excluded from scheduling) and it'll release the lock, so that other threads can acquire it. When you call signal, then one thread in the wait list of the monitor is being resumed.