Lock and Condition in Java
Java, interface Lock defines a method newCondition().
method returns an object of type Condition.
is a device to partition waiting threads into multiple groups such
that threads belonging to one group can be notified separately from
other sets’ threads.
as Lock is a fine tuned replacement for synchronized, Condition is a
fine tuned replacement for monitor.
helps all threads to go into waiting state and only one of them can
enter the critical section (like synchronized(monitor) block which
allows only one thread to go into the criitcal section).
helps a thread in the critical section to give up the lock and go
into waiting state till its notified (like monitor.wait() which does
exactly the same thing).
offers a finer control by allowing several conditions per lock so
that threads can wait/notified in groups (rather than just
notify/notifyAll in normal monitors)
2 methods put() and take() are both guarded by lock() and unlock()
calls on the same Lock object. This means that if a producer is
putting an object, a consumer cannot take the object at the same time
and vice versa. We cannot allow both producer and consumer to be
active at the same time because both attempt to change the count
property. So both are doing a write operation on count property and
so cannot do that simultaneously.
locking mechanism is equivalent to synchronized(lock) functionality.
buffer is full, multiple writers may be waiting.
notify one of the writers, we use notFull.signal()
that use of two different Condition variables helps us to put writers
in one waiting set and readers in other waiting set.
we were using plain monitors, then this separation would not have
been possible. We would had to use notify() or notifyAll() which
would awake any thread among the readers or writers.
summarize, Condition helps to partition waiting threads into groups
such that each group can be woken up indepently of the other groups.