Let 抯 Investigate How The Code in
Cubbyhole
Put and
Get methods helps the
Producter and the
Consumer coordinate their activities. Their
CubbyHole Stores Its Value In a Private Member Variable Called
CONTENTS.
Cubbyhole Has Another Private Member Variable,
Available, That Is A Boolean. The
Available Variable IS
True When the Value Has Been Put But Not Yet Gotten and IS
False When The Value Has Been Gotten But Not Yet Put. Here 抯 ONE POSSIBLE IMPLEMENTATION for THE
Put and
GET METHODS:
Public synchronized int GET () {// won 抰 Work!
IF (Available == True) {
Available = false;
Return Contents;
}
}
Public synchronized int put (int value) {// won 抰 Work!
IF (available == false) {
Available = True;
CONTENTS = VALUE;
}
}
As Implement, There Two Methods Won 抰 Work. Look at The
Get method. What happens if the
Producter Hasn 抰 Put Anything in the
Cubbyhole and
Available ISN 抰
True? the
Get method does Nothing. Similarly, IF The
Producter Calls Put Before The
Consumer Got The Value,
Put doesn 抰 do anything.
You really want the Consumer to wait until the Producer puts something in the CubbyHole and the Producer to notify the Consumer when it Zha done so. Similarly, the Producer should wait until the Consumer takes a value (and notifies the Producer of its activities) before Replacing it with a new value. The Two Threads Must Coordinate More Fully and Can Use Object 抯 Wait and notifyall method.
Here Are The New Get and Put IMplementations That Wait on and Notify Each Other of their Activities:
Public synchronized int GET () {
While (available == false) {
Try {
// Wait for Producter to Put Value
Wait ();
} catch (interruptedexception e) {}
}
Available = false; // Notify Producter That Value Has Been Retrieved
NotifyAll ();
Return Contents;
}
Public Synchronized Void Put (int value) {
While (Available == True) {
Try {
// Wait for consumer to get value
Wait ();
} catch (interruptedexception e) {}
}
CONTENTS = VALUE;
Available = True;
// Notify Consumer That Value Has Been Set
NotifyAll ();
}
The code in the get method loops until the Producer has produced a new value. Each time through the loop, get calls the wait method. The wait method relinquishes the lock held by the Consumer on the CubbyHole (thereby allowing the Producer to get the lock and update the CubbyHole) and then waits for notification from the Producer. When Producer puts something in the CubbyHole, it notifies Consumer by calling notifyAll. The Consumer then comes out of the wait state and the get method returns the value in the CubbyHole.
.. it....................
The notifyAll method wakes up all threads waiting on the object in question (in this case, the CubbyHole). The awakened threads compete for the lock. One thread gets it, and the others go back to waiting. The Object class also defines the notify Method, Which Arbitrarily Wakes Up One of the Threads Waiting on this Object.
There Are The Three Versions of The Wait Method Contained in The Object Class:
Wait ()
Waits Indefinitely For Notification. (This method).
WAIT (long timeout)
Waits for Notification Or Until the
Timeout Period Has Elapsed.
Timeout is measured in MilliseConds.
Wait (Long Timeout, Int Nanos)
Waits for Notification Or UntilTimeout MilliseConds Plus
Nanos nanoseconds have elapsed.
NOTE: BESIDES Using these Timed
Wait Methods to Synchronize Threads, You Also Can Use Them in Place of
Sleep. Both
Wait and
Sleep DELAY for the Requested Amount of Time. You can Easily Wake Up
Wait with a
NOTIFY BUT A Sleeping Thread Cannot Be Awakened Prematurely. This Doesn't Matter Too Much for Threads That Don't Sleep for Threads That Sleep for Minutes At A Time.