A developer synchronized access to a `HashMap` to prevent race conditions. What is the primary performance drawback of this approach compared to using a `ConcurrentHashMap`?
Java interview question for Advanced practice.
Answer
It prevents all concurrent access, acting as a bottleneck because only one thread can operate on the map at a time.
Explanation
The primary drawback of a synchronized map is that it uses a single, global lock for the entire object. This means only one thread can read from or write to the map at any given moment, regardless of whether threads are accessing different keys. This creates a major bottleneck and severely limits scalability. In contrast, ConcurrentHashMap uses granular locking, allowing multiple threads to operate on different parts of the map simultaneously, leading to much higher throughput.