7.3. Synchronization

An important consideration when designing multi-threaded applications is conflict over access to data. If two threads are fighting for the same resource, and a mechanism to resolve access conflicts is not put into place, the integrity of the application is at stake. Built into the Java language are two mechanisms for preventing concurrent access to resources: method-level synchronization and block-level synchronization.

7.3.1. Method-Level Synchronization

Method-level synchronization prevents two threads from executing methods on an object at the same time. Methods that must be “thread-safe” are marked as synchronized. When a synchronized method of an object is invoked, a thread takes out an object lock, or monitor. If another ...

Get Java™ Network Programming and Distributed Computing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.