liveBook · Manning

Synchronization

As explained in Advanced Algorithms and Data Structures

Synchronization is the coordination of concurrent operations to control access to shared state and preserve required execution properties.

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

How Advanced Algorithms and Data Structures covers it

How deep it goes
Explains race interleavings, atomicity, locks, and Java implementation choices, but stops short of general synchronization algorithms.
Examples
Uses concurrent LFU cache insertions, capacity checks, lock acquisition, ConcurrentHashMap, and a synchronized heap.
What it assumes
Assumes basic cache operations and threads while developing race conditions and atomicity through a concrete example.
What you can do after
Diagnose race conditions and design lock-protected cache operations using Java concurrency structures.
How it connects
Connects race conditions with atomicity, isolation, locks, concurrent data structures, deadlocks, and cache eviction.

Also explained in

Compare how 11 books explain synchronization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.