![]() |
Help: Using Wait and Notify in a Barrier Scenario
Help is available for each task, or you can go straight to the solution source code.
Add the int variable countOfCounters to the MasterCounter class. Have it initialized to zero, and increment it in the newCounter method every time a new Counter is created. Task 2Add 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.Add the new method, it should take a Counter's current count as an argument, and return the integer value that will be the sum of all Counters' current counts. Add an instance variable to the MasterCounter class to keep track of the number of Counters waiting, also add a variable to keep track of the sum. Each time the method is called, add the current count to the sum variable. If the number of Counter's waiting is one less than the number of Counters, (which means it is the last Counter to call the method) call notifyAll and return the sum. Otherwise, increment the countersWaiting variable and call wait. Task 3Modify the Counter class' run method to call getTotalCount after the incrementing calculation is done, and add the result to the Counter's count variable.In the run method, after completing the incrementing loop (but before the call to counterDone, call getTotalCount and add it to the count variable. |
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |