Synchronization coordinates concurrent operations on shared state so compound actions remain safe. Without it, two cache updates can both observe a free slot and exceed the configured capacity. section 7.7
The central issue is atomicity: checking capacity, evicting an entry, inserting a node, and updating the hash table must not interleave unsafely with another operation. A second check only reduces the chance of failure; it does not make the set operation atomic. section 7.7
A synchronized cache typically:
- Protects writes with a lock; a competing thread waits until the current lock holder releases it. section 7.7
- Uses concurrent internal structures when appropriate, such as ConcurrentHashMap and a synchronized heap. section 7.7.1
- Handles synchronization carefully, because combining method-level locking with concurrent structures can introduce bugs or deadlocks. section 7.7.1
Two writers, one remaining cache slot
Two threads call set concurrently on a cache with maxSize - 1 entries. Both pass the capacity check before either inserts, so both add an entry. A lock serializes the critical operation: one thread writes first, while the other waits and then observes the updated state. section 7.7 section 7.7










