![]() |
Using a Facade to avoid Deadlocks
PrerequisitesWhen using synchronized methods, one has to be aware of potential deadlocks. One way to avoid them is to use a centralized locking scheme for some groups of objects. This Magercise shows how to use a facade object to act as a global lock for a group of objects.The situation here involves objects representing persons and lockers. Each person may have a locker, and each locker a person as its owner. The goal of the design is to maintain referential integrity whenever a modification is made to a person-locker relationship. The given starter file just uses synchronized methods on the Person and Locker objects to avoid referential integrity problems. However a deadlock situation can occur as a change in relationship may require obtaining locks on two Person objects, and in a concurrent environment this can result in a deadlock. Modify the solution by adding a Registrar object to handle all relationship changes. By providing a single point of entry for obtaining the necessary locks, the Registrar object will keep deadlocks from occurring. Various techniques for safe programming with threads are covered by the course notes section Ensuring Safety and Liveness. Work LocationPerform all work for this magercise in VisualAge project MageLang Magercises, package magercises.Using a Facade to Avoid Deadlocks. 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:
Modify the LockerSwitcher class to use the Registrar for switches. Modify the FacadeTest class to use the Registrar object.
|
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |