![]() |
Synchronized Statements
PrerequisitesThis Magercise shows a case where synchronizing on an object works more conveniently than using synchronzied methods. Here a Speaker object wants to get a lock on an AnnounceBox and hold it across several method calls, thus avoiding the interference of other threads. This technique is also useful when accessing objects that are provided by others and don't have any built-in thread safety.Work LocationPerform all work for this magercise in VisualAge project MageLang Magercises, package magercises.Synchronized Statements. 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:
Compile and run the applet to demonstrate contention for the AnnounceBox. Change the Speaker class to synchronize on the AnnounceBox before writing to it using a synchronized statement. Compile and run the applet, both sentences should be uninterrupted. AnnounceBox.
|
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |