Lock and Condition in Java
In
Java, interface Lock defines a method newCondition().
This
method returns an object of type Condition.
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.
Just
as Lock is a fine tuned replacement for synchronized, Condition is a
fine tuned replacement for monitor.
Lock.lock()
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).
Condition.await()
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).
Condition
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)
Example:
The
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.
This
locking mechanism is equivalent to synchronized(lock) functionality.
If
buffer is full, multiple writers may be waiting.
To
notify one of the writers, we use notFull.signal()
Note
that use of two different Condition variables helps us to put writers
in one waiting set and readers in other waiting set.
If
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.
To
summarize, Condition helps to partition waiting threads into groups
such that each group can be woken up indepently of the other groups.
|