The BackendDeeper

Kayavak / The Backend / BE·38 Concurrency / Deeper

Deeper · The Backend

Concurrency

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Java had threads from version 1.0 (1996), at a time when most machines had one processor and threads mainly kept interfaces responsive. Its original memory model, the rules for when one thread sees another's writes, turned out to be broken: researchers including Bill Pugh showed that common idioms (like double-checked locking) failed and that the specification forbade optimisations every JVM performed. JSR 133 rewrote it for Java 5 (2004) around the happens-before relation, and Doug Lea's java.util.concurrent arrived in the same release with executors, locks, atomics and concurrent collections.

Multicore processors then made concurrency unavoidable, and threads became the bottleneck for servers: each platform thread costs about a megabyte of stack and an operating-system thread, so tens of thousands of waiting connections meant tens of thousands of threads. Reactive frameworks avoided threads at the cost of harder code. Virtual threads (Project Loom, final in Java 21, 2023) made threads cheap again, so blocking, sequential code scales.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

Threads share memory, and without synchronisation there's no guarantee one thread sees another's writes, or sees them in order. Synchronisation creates happens-before edges; everything outside those edges is a race.

Question Answer
What is it? Running several tasks at overlapping times, sharing memory, safely
Problem it solves Using many cores and waiting on many I/O operations at once
Parts Threads (platform and virtual), the memory model, locks, volatile, atomics, executors, concurrent collections, structured concurrency
How it works Locks and volatile accesses order memory operations across threads; executors schedule tasks onto threads
Assumptions Every shared mutable variable is protected by one discipline
When they change An unprotected counter loses updates; a flag without volatile may never be seen to change
Where it fails Deadlocks, contention, and bugs that appear only under load or on other hardware
Related ideas OS threads and deadlock (Foundations, ch08); caches and coherence (Foundations, ch07); distributed systems

What if?

What if two threads increment an int counter a million times each, unsynchronised?

Reason it through, then open

The total will usually be less than two million. count++ is read, add, write; two threads read the same value, both add one, both write, and one increment is lost. The exact shortfall changes every run. AtomicInteger, LongAdder or a lock make the read-modify-write indivisible.

What if virtual threads made locks unnecessary?

Reason it through, then open

They don't: virtual threads change how cheap threads are, not how they share memory. Races and deadlocks are exactly as possible. What changes is that blocking a thread costs almost nothing, so you can write simple blocking code for I/O instead of callbacks, and use one virtual thread per task rather than a tuned pool.

Where the boundary is

Aspect Concurrency Parallelism
Means Several tasks in progress at once Several tasks executing at the same instant
Needs multiple cores No Yes
Typical goal Handle many waiting things (I/O) Finish CPU work faster
Example A server with 10,000 open connections Sorting a large array on 8 cores

Derive it

Amdahl's law: why more cores give diminishing returns.

  1. Let a program take time 1 on one core, of which a fraction p can run in parallel and 1 − p can't.
  2. On n cores the parallel part takes p/n; the serial part still takes 1 − p.
  3. Speedup = 1 / ((1 − p) + p/n).
  4. As n grows without limit, p/n goes to zero, so speedup approaches 1 / (1 − p).
  5. If 95% is parallel, no number of cores gives more than 20× (1 / 0.05). The serial 5% (a lock everyone takes, a single-threaded step) sets the ceiling, which is why reducing contention often matters more than adding cores.

How sure are we?

Claim How it's known Where from
The Java memory model was rewritten for Java 5 (JSR 133) Historical JSR 133, 2004
Speedup is bounded by 1/(1 − p) Theorem Amdahl, 1967, under its assumptions (fixed problem size)
Unsynchronised increments lose updates Measured The chapter's lab; reproducible on any multicore machine
Virtual threads scale blocking I/O code Measured JEP 444's benchmarks and the chapter's lab; CPU-bound work doesn't benefit

Still open

Structured concurrency (treating a group of tasks as one unit with shared cancellation and errors) is still settling in Java's API. Gustafson's law (1988) points out that people use more cores to solve bigger problems, which softens Amdahl's ceiling; which framing fits depends on the workload.

What would change your mind? If a program's speedup kept growing past 1/(1 − p), your estimate of the serial fraction was wrong, or the problem grew with the cores.

Recall

What does Amdahl's law say?

Speedup on n cores is 1/((1 − p) + p/n), so the serial fraction caps it at 1/(1 − p).

Why does an unsynchronised `count++` lose updates?

It's a read, add and write; two threads can read the same value and both write the same result.

What creates a happens-before edge in Java?

Locks, volatile accesses, thread start and join, and the concurrent utilities built on them.

Do virtual threads remove the need for synchronisation?

No; they make threads cheap, but shared memory still needs protection.

What's the difference between concurrency and parallelism?

Concurrency is tasks in progress at once; parallelism is tasks executing at the same instant.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.