What is Condition interface?
java.util.concurrent.locks.Condition interface is found in java.util.concurrent.locks package. Condition instance are similar to using Wait(), notify() and notifyAll() methods on object.
Difference between traditional synchronization and Condition interface in java.
- In case of traditional synchronization, there is only one object monitor so we can have only single wait-set per object. But,
Condition instance are used with Lock instance, Condition factors out the Object monitor methods ( Wait(), notify() and notifyAll()) into distinct objects to give the multiple wait-sets per object.
- Example > In the program we we created producerCondition and consumerCondition from Lock. And thread rather than acquiring and releasing lock on producer and consumer object as done in previous tutorials, acquired and released lock on producerCondition and consumerCondition. Hence, enabling multiple wait-sets per object.
RELATED LINKS>
Locks and ReEntrantLocks in java
Implementation of custom/own Lock and ReEntrantLock in java
ReentrantLock class provides implementation of Lock’s newCondition() method - description and solving producer consumer program using this method
Labels:
Core Java
Thread Concurrency