Backend Development 31 min read

Deep Dive into Java HashMap, ConcurrentHashMap, JMM, and Concurrency Fundamentals

This article provides a comprehensive overview of Java's HashMap and ConcurrentHashMap implementations, explains core concurrency concepts such as thread states, the differences between concurrency and parallelism, memory models, volatile semantics, singleton patterns, thread pools, ThreadLocal, CAS, and the AQS framework.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Deep Dive into Java HashMap, ConcurrentHashMap, JMM, and Concurrency Fundamentals

HashMap

Key features include unordered storage, support for null keys and values, lazy initialization, and an internal structure that evolves from an array + linked list (JDK7) to array + linked list + red‑black tree (JDK8). Initial capacity and load factor are critical for performance, and bins may convert to a red‑black tree during heavy collisions.

ConcurrentHashMap

In JDK7 it is built on Segment + HashEntry with segment‑level locks; the number of segments determines concurrency and cannot be expanded after initialization. In JDK8 the segment lock is removed in favor of CAS + synchronized , the entry type changes to Node , and a red‑black tree is introduced for high‑collision bins. Read operations are lock‑free, while write operations use CAS and synchronized blocks; size() may fall back to full locking for accuracy.

Concurrency Basics

Concurrency means multiple threads operating on the same resource, often on a single‑core CPU with rapid context switches, whereas parallelism uses multiple CPU cores to execute tasks truly simultaneously. The article lists the six Java thread states (New, Runnable, Running, Blocked, Waiting/Timed‑Waiting, Terminated) and common ways to create threads: extending Thread , implementing Runnable or Callable , using thread pools, and Spring's @Async annotation.

Thread Synchronization

It explains wait / notify requiring the object's monitor, the difference between synchronized (monitor‑based) and explicit locks, lock upgrade paths (biased → lightweight → heavyweight), and why synchronized cannot prevent instruction reordering but guarantees as‑if‑serial execution. Volatile semantics are covered, including memory barriers (StoreStore, StoreLoad, LoadLoad, LoadStore) and the happens‑before relationship.

Singleton Pattern

Double‑checked locking (DCL) combined with volatile prevents instruction reordering that could expose a partially constructed instance. The article also mentions eager initialization and enum‑based singletons as alternatives.

Thread Pool

The seven core parameters (corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit, BlockingQueue, threadFactory, RejectedExecutionHandler) are mapped to a banking‑counter analogy. It warns against using Executors factory methods that create unbounded queues, which can cause OOM, and recommends constructing ThreadPoolExecutor directly with a bounded queue.

private static ExecutorService executor = new ThreadPoolExecutor(10, 10, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue(10));

ThreadLocal

ThreadLocal stores a value per thread using a ThreadLocalMap where the key is a weak reference, allowing the map entry to be reclaimed when the ThreadLocal object is no longer reachable, thus avoiding memory leaks.

CAS and AQS

CAS (compare‑and‑swap) provides atomic updates using three operands (address, expected value, new value) but suffers from ABA, high spin cost, and limited applicability. The AbstractQueuedSynchronizer (AQS) builds higher‑level synchronizers (e.g., ReentrantLock , Condition ) on top of CAS and volatile fields, managing a synchronization queue and a waiting queue.

Additional Topics

Brief coverage of thread‑local storage, spin locks vs heavyweight locks, Amdahl's law for performance scaling, and a list of typical interview questions on these subjects.

JavaconcurrencythreadpoolHashMapvolatileJMMsingleton
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.