![]() |
Help: Using Wait and Notify in a Producer/Consumer Scenario
Help is available for each task, or you can go straight to the solution source code.
Replace the check for an empty stack with a while loop, whose conditional tests the size of the stackVector and calls wait while it is null. Remember that any call to wait must catch InterruptedException. After exiting the while loop, get the last element of stackVector, remove it from the Vector and return it as normal. Be sure not to cache the value of stackVector's size, as it will probably change during a wait. Task 2Modify the push method to notify all waiting threads. Rename SimpleStack to WaitStack.Add a call to notifyAll after the addElement call in the push method. Task 3Create two new classes, StackPusher, which acts a producer task by repeatedly pushing an item onto the stack, and StackPopper, which acts as a consumer, and repeatedly pops items off the stack. As the last thing in the run method of both classes, make a call to StackTest.threadDone to print the stack to the output TextArea. The StackTest Applet class shows how the constructors of these classes should look.These classes are very similar to StackStuffer in the Synchronized Methods Magercise. Have the StackStuffer push its item onto the stack ten times, have the StackPopper pop five items off the stack. |
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |