Java Reference
In-Depth Information
must release the object's monitor before any threads that are blocked in the entry set can have the ownership of the
object's monitor. A patient may free the doctor for one of two reasons:
The patient is done with his treatment and he is ready to go home. This is a straightforward
case of a patient freeing the doctor after his treatment is over.
A patient is in the middle of his treatment. However, he must wait for some time in order for
the doctor to resume his treatment. Let's assume that the clinic has a special waiting room
(separate from the one where patients who just signed in wait) for those patients who are in
the middle of their treatment. This case needs some explanation. Let's say that the doctor is an
eye specialist and he has some patients in his clinic. The patient who is being treated needs an
eye examination for which his pupils must be dilated first. It takes about 30 minutes after the
patient receives eye drops for full pupil dilation, which is required for the examination. Should
the doctor be waiting for 30 minutes for the patient's pupils to dilate? Should this patient release
the doctor for 30 minutes and let other patient have access to the doctor? You would agree that
if doctor's time can be used to treat other patients while this patient's pupils are being dilated, it
is fine for this patient to release the doctor. What should happen when once this patient's pupils
are dilated, however, and the doctor is still busy treating another patient? The doctor cannot
leave any patient in the middle of treatment. Therefore, the patient who released the doctor and
waited for some condition to be true (here dilation process to complete) must wait until doctor
is free again. I will have more explanations on this issue later in this chapter and I will try to
correlate this situation with threads and the object's monitor lock.
I must discuss another issue in the context of the doctor-patients example before I can compare this with
monitor-threads case. When the doctor is free and only one patient is waiting to get access to him, there is no
problem. The sole patient waiting for the doctor will get access to him immediately. However, what happens when the
doctor becomes available and there is more than one patient waiting to get access to him? Which one of the waiting
patients should get access to the doctor first? Should it be the patient who came first (First In, First Out or FIFO)?
Should it be the patient who came in last (Last In, First Out or LIFO)? Should it be the patient who needs the least
(or the most) amount of time for his treatment? Should it be the patient who is in the most serious condition?
The answer is that it depends on the policy followed by the clinic management.
Similar to a patient in the doctor-patients example, a thread can also release an object's monitor lock for
two reasons:
At this time, the thread has completed the work for which it had acquired the object's
monitor lock. The arrow “Release and exit” indicates this scenario in the diagram. When
a thread simply exits a synchronized method/block, it releases the object's monitor lock it
had acquired.
The thread is in the middle of a task and it needs to wait for some condition to be true to
complete its remaining task. Let's consider the producer/consumer problem. Suppose the
producer acquires the buffer object's monitor lock and wants to write some data into the
buffer. However, it finds that the buffer is full and the consumer must consume the data and
make the buffer empty before it can write to it. In this case, the producer must release the
buffer object's monitor lock and wait until the consumer acquires the lock and empties the
buffer. The same logic applies for the consumer when it acquires the buffer's monitor lock
and finds that buffer is empty. At that time, the consumer must release the lock and wait until
the producer produces some data. This kind of temporarily releasing of the object's monitor
lock and waiting for some condition to occur is shown in the diagram as the “Release and
wait” arrow. An object can have multiple threads that can be in “Release and wait” state at the
same time. All threads that have released the object's monitor lock and are waiting for some
conditions to occur are put in a set called a wait set .
Search WWH ::




Custom Search