Writing efficient thread-safe classes
Synchronized keyword
While most of the activities in a multi-threaded application can be done in parallel, some actions, such as updating a global flag or manipulating a shared file, cannot. In such cases, a lock must be acquired to prevent other threads from accessing the same method until the thread performing the action is finished. This lock is provided in Java programs via the synchronized keyword. Listing 1 illustrates the usage.
While most of the activities in a multi-threaded application can be done in parallel, some actions, such as updating a global flag or manipulating a shared file, cannot. In such cases, a lock must be acquired to prevent other threads from accessing the same method until the thread performing the action is finished. This lock is provided in Java programs via the synchronized keyword. Listing 1 illustrates the usage.
Comments