A consistent locking policy guarantees that multiple threads cannot simultaneously access or modify shared data. If two or more operations need to be performed as a single atomic operation, a consistent locking policy must be implemented using either intrinsic synchronization or java.util.concurrent
utilities. In the absence of such a policy, the code is susceptible to race conditions.
Given an invariant involving multiple objects, a programmer may incorrectly assume that individually atomic operations require no additional locking. Similarly, programmers may incorrectly assume that using a thread-safe Collection
does not require explicit synchronization to preserve an invariant that involves the collection's elements. A thread-safe class can only guarantee atomicity of its individual methods. A grouping of calls to such methods requires additional synchronization.
...