![]() |
Using Wait and Notify in a Barrier Scenario
PrerequisitesIn this Magercise the methods wait and notifyAll are used to synchronize a group of threads.Three runnable instances of a Counter class will simulate a complex calculation by repeatedly incrementing a variable until it reaches a predetermined maximum. Each thread will finish at a different time. A MasterCounter class is used to manage the Counters. The goal of the Magercise is to implement the method getTotalCount for the MasterCounter class. This method will act as a barrier, forcing all Counters to wait until the last is done with its calculation. It then tallies the total value of all the Counter objects and returns that value. The Counter objects then add that value to their own count. Wait and notify are covered in the course notes section Condition Synchronization. Work LocationPerform all work for this magercise in VisualAge project MageLang Magercises, package magercises.Using Wait and Notify in a Barrier Scenario. If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name. Tasks
Perform the following tasks:
Add a new method getTotalCount, to the MasterCounter class. This method should act as a barrier, making all threads who call it wait until the last thread calls this method, in which case it will call notifyAll and the method will return the sum of all the Counter objects' count values. Modify the Counter class' run method to call getTotalCount after the incrementing calculation is done, and add the result to the Counter's count variable.
|
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |