Java Reference
In-Depth Information
public void someMethod_3() {
// other statements go here
synchronized(objectRef) {
// Current thread possesses monitor lock of objectRef
while (some condition is true) {
// It is ok to call the wait() method on objectRef because
// the current thread possesses monitor lock on objectRef
objectRef.wait();
}
}
// other statements go here
}
}
Note that objectRef is an instance variable and it is of the type java.lang.Object . Its only use is to synchronize
threads' access to a block inside the someMethod_3() method. Since it is declared an instance variable, all threads
calling someMethod_3() will use its monitor to execute the synchronized block. A common mistake made by
beginners is to declare objectRef as a local variable inside a method and use it to in a synchronized block. The
following snippet of code shows such a mistake:
public void wrongSynchronizationMethod {
// This objectRef is created every time a thread calls this method
Object objectRef = new Object();
// It is a blunder to use objectRef for synchronization below
synchronized(objectRef) {
// In fact, this block works as if there is no synchronization, because every
// thread creates a new objectRef and acquires its monitor lock immediately
}
}
With the above snippet of code in mind, you must use an object reference that is common to all threads to
synchronize access to a block.
Let's get back to the question of which patient will get access to the doctor when he becomes available again. Will
it be a patient from the waiting room who is waiting after signing in or a patient from another waiting room who was
waiting in the middle of his treatment? Before you answer this question, let's make it clear that there is a difference
between the patients in the waiting room who are waiting after signing in and the patients waiting for some condition
(e.g. dilation to complete) to occur in another waiting room. After signing in, patients wait on the availability of the
doctor, whereas patients in the middle of their treatments wait on a particular condition to occur. For patients in
the second category, a particular condition must hold before they can seek access to the doctor, whereas patients in
the first category are ready to grab access to the doctor as soon as possible. Therefore, someone must notify a patient
in the second category that a particular condition has occurred and it is time for him to seek access to the doctor
again to continue his treatment. Let's assume that this notification must come from a patient being currently treated
by the doctor. That is, the patient who currently has access to the doctor notifies the patients waiting in the middle of
their treatments to get ready to gain access to the doctor again. Note that it is just a notification that some condition
has occurred and it is delivered only to the patients waiting in the middle of their treatments. Whether the patient
in the middle of his treatment will get access to the doctor right after the current patient is done with the doctor is
not guaranteed. It only guarantees that the condition on which a patient was waiting holds at the time of notification
and the waiting patient may try to get access to the doctor to continue his treatment. Let's correlate this example to
monitor-threads example.
Search WWH ::




Custom Search