Collectionsmedium3-5 years

How is ConcurrentHashMap different from Collections.synchronizedMap, and what is still not atomic?

Lock-free reads and per-bucket write locking versus one lock for everything; atomic compound methods (computeIfAbsent, merge, putIfAbsent) versus none; weakly consistent iteration versus fail-fast-unless-you-lock. Two separate calls are never atomic on any map — if (!containsKey) put still races.

The lesson behind it →