Lesson · Multithreading Fundamentals
Processes vs Threads: isolation, sharing and the speed-up ceiling
15 min readProcesses vs Threads
A process is the operating system's unit of isolation; a thread is the unit it schedules. Java runs one process with many threads, so a static field is a conversation between them and a second process is a stranger. This lesson draws both pictures, then works out why the serial fraction of your code caps every speed-up.
One program, several things at once
A server has to read a socket, query a database and write a response. It should not do them strictly one after another while the next request waits, so the operating system offers two ways to make a program do more than one thing: a second process, or a second thread.
The two are not interchangeable. Choosing between them decides what the pieces share, what they must pay to talk, and what a crash takes down with it. Get the model right once and the rest of this part of the course is easier to place.
A process is an isolation boundary
A process owns an address space: its own code, its own heap, its own stacks, its own file descriptors. Nothing in another process can read that memory directly, and the protection is enforced by the hardware, not by convention or by discipline.
Isolation doubles as the crash boundary. When a process dies, the operating system reclaims its memory and everything else keeps running. That is why browsers put each tab in its own process, and why a JVM crash does not corrupt the database next to it.
The bill arrives twice. Two processes talk over pipes, sockets or explicitly mapped memory, so every payload is copied or encoded at the boundary. And switching between processes costs more than switching between threads, because the CPU has to change address space and the caches that describe it go cold.
- Owns: address space, open files, environment, exit status. The operating system gives each process its own copy of the world.
- Talks via IPC: pipes, sockets, files, shared memory. There is no shared field to write to, which is exactly the safety property.
- Crash boundary: one process dying cannot corrupt another, because the memory is not reachable in the first place.
A process is isolation you can rely on but cannot share through. Every byte crossing the boundary is copied or encoded, and paying that cost is what makes the boundary real.
A thread is a scheduling unit inside a process
A thread is a single stream of execution the scheduler can run independently: its own program counter, its own stack, its own copies of the registers. Everything else belongs to the process, so every thread sees the same heap, the same static fields, the same class metadata and the same open files as its siblings.
That sharing is the entire appeal. One thread hands data to another by writing a field: no serialisation, no copy, no round trip through the operating system. It is also the entire bug surface, because the race conditions lesson is simply what happens when two threads write the same field without arranging an order.
| Dimension | Process | Thread |
|---|---|---|
| Unit of | isolation: its own address space | scheduling: executes inside a process |
| Shares | nothing by default | heap, statics, open files, code |
| Owns privately | address space, resources | stack, program counter, registers |
| Talking to a peer | IPC: pipe, socket, shared memory | a field write, or a queue |
| Switch cost | higher: address space and caches change | lower: the address space stays |
| On failure | takes that process down | can corrupt shared state or kill the process |
Threads share memory; that sharing is both the speed and the bug surface. The same field write that makes threads useful is the one that needs an ordering argument.
The memory picture
Draw two processes side by side and their code, heap and stacks sit inside each one as private blocks, unreachable from the other. Zoom into one process and the drawing changes shape: the heap is drawn once because every thread uses it, while each thread gets its own stack beside it.
Keep that picture while you read Java code. A local variable and a parameter live on your stack, so no other thread can see them. An object lives on the heap and is reachable from every stack that holds a reference to it.
Memory layout · processes and threads
privatesharedExclusion is the product: nothing in one process reaches the other, so anything two processes share has to travel over a channel.
One address space held by the operating system. A crash here is contained here.
Same picture, different memory. Identical code, independent state.
Inside Process A · threads
objects, arrays, class metadata
frames, locals, PC, registers
frames, locals, PC, registers
Each stack belongs to exactly one thread: its calls, its locals, its program counter. Every thread reads and writes the one heap above.
Where Java sits on the picture
A running JVM is one process. Your objects, your static fields and your class metadata all live in that process's memory, and java.lang.Thread is a platform thread: a real operating system thread, scheduled 1:1 by the kernel.
Virtual threads (Java 21) keep the same API while changing the mapping. Many virtual threads are multiplexed onto a small pool of platform carrier threads, so blocking a virtual thread is cheap. That is a later part of the course; for now, one new Thread(...) means one operating system thread.
| Concept | What the OS sees | What Java calls it |
|---|---|---|
| Running JVM | one process | the program, plus its heap and metaspace |
| Thread of execution | one OS thread | java.lang.Thread (platform thread) |
| Java 21 virtual thread | not an OS thread | Thread.ofVirtual(), multiplexed onto carriers |
| Data shared between threads | the same address space | fields, statics, arrays, collections |
| Data shared between processes | a channel you build | pipe, socket, memory-mapped file |
Concurrency is structure, parallelism is hardware
Concurrency is a way of structuring a program as several independent activities: a reader, a writer, a watchdog. It works on a single core, because the scheduler interleaves the threads fast enough for the program to keep making progress.
Parallelism is different in kind. It means two or more of those activities executing at the same instant, which needs two or more cores. A program can be perfectly concurrent (correct, responsive, built from tasks) and get zero parallelism from a one-core machine.
The distinction matters because the two are usually discussed as one. Sixteen threads on four cores do not deliver sixteen times the work: they deliver the parallelism the hardware offers, organised by the structure you chose.
Concurrency is about structure; parallelism is about hardware. The first is a design decision you make, the second is a property of the machine you are given.
The serial fraction sets the ceiling
Suppose a fraction p of the work can run in parallel and the remaining 1 - p must stay serial. More workers shrink the parallel part and never touch the serial part, so the speed-up converges instead of growing: S(N) = 1 / ((1 - p) + p / N), with ceiling 1 / (1 - p) as N grows without limit.
At p = 0.95 the ceiling is 20x, and most of it is paid for early: sixteen workers deliver about 9.14x, sixty-four only about 15.42x. The remaining 5% is the wall, and no worker count moves it.
Coordination pushes the measured result below even that curve. Locks turn part of the parallel section serial again, queues add handoffs, and shared writes have to reach every core coherently. Amdahl is the optimistic bound, not the forecast.
| Parallel fraction p | Ceiling 1 / (1 - p) | At 4 workers | At 16 workers | At 64 workers |
|---|---|---|---|---|
| 0.50 | 2.00x | 1.60x | 1.88x | 1.97x |
| 0.90 | 10.00x | 3.08x | 6.40x | 8.77x |
| 0.95 | 20.00x | 3.48x | 9.14x | 15.42x |
| 0.99 | 100.00x | 3.88x | 13.91x | 39.26x |
- Split the work: 5% of the time is serial, so (1 - p) = 0.05, and p = 0.95.
- Substitute for N = 16: S = 1 / (0.05 + 0.95 / 16) = 1 / 0.109375, which is about 9.14.
- Compare with the ceiling, 1 / 0.05 = 20. Sixteen workers bought 9.14 of the available 20, and sixty-four buy 15.42. The curve is flattening toward the wall, not toward infinity.
Amdahl lab · S(N) = 1 / ((1 - p) + p / N)
pick a parallel fraction and a worker count1 / ((1 - 0.95) + 0.95 / 16)
At p = 95%, 16 workers reach 9.14x. The serial 5% caps every worker count at 20.00x, so even a thousand cores would stop there.
Switching and coordination are not free
A core runs one thread at a time, so threads outnumbering cores mean context switches: the scheduler saves one thread's registers and load state and restores the next one's. Between threads of one process the address space stays put, but the caches still go cold and the switch costs time in the middle of real work.
Coordination costs more than switching. A lock converts part of the parallel region back into serial work, which is the same as shrinking the effective p that Amdahl sees; queues add handoffs between threads, and every shared write has to become visible to the other cores. That is why an honest measurement usually lands below the Amdahl number, and why the right move is to measure rather than multiply.
The serial fraction sets the ceiling, not the core count. Add workers until the curve flattens, then go and shrink the serial part instead of buying more of what is already idle.
The ceiling is arithmetic, not hardware
Fill in the numbers before you size a machine: 5% serial work caps the program at 20x, whatever the core count, and 50% caps it at 2x. Candidates who quote a core count as a speed-up have not done this arithmetic, and the follow-up question is always to do it live.
Interview angles
What is shared between two threads, and what is not?+
They share the process: heap, static fields, class metadata, open files. Each thread has its own stack, program counter and register state, which is why locals and parameters are private to it.
Common misconception: The common wrong answer is that each thread has its own copy of the heap. It is the other way round: the stack is private and the heap is shared, and that is exactly why shared mutable state is the hazard.
Why does a context switch between processes cost more than one between threads of the same process?+
The CPU has to change address space: page tables, the memory map and the translation caches pointing at them. Threads of one process keep the address space, so the switch is cheaper, though caches still cool down either way.
Concurrency and parallelism: the same thing?+
No. Concurrency is structuring a program as independent activities that may be interleaved; it works on a single core. Parallelism is running some of them at the same instant, which needs several cores. A concurrent design yields zero parallelism on a one-core machine.
Yes, for structure and for responsiveness: a blocked disk read or socket can overlap with computation because the scheduler runs the other thread meanwhile. That is concurrency without parallelism, and it is often the actual requirement.
Follow-up: So does a single-core machine ever benefit from threads?
Amdahl's law: 10% of the work is serial. What is the maximum speed-up?+
10x. The ceiling is 1 / (1 - p) = 1 / 0.1 regardless of the worker count. Four workers give about 3.08x, sixteen about 6.40x, sixty-four about 8.77x, all converging on 10x.
Measured numbers come in lower because locks, queues and cache coherence convert part of the parallel section into serial work, and because the switch and handoff costs grow with the thread count. Amdahl is a bound, not a promise.
Is one Java thread one operating system thread?+
For platform threads, yes: java.lang.Thread maps 1:1 onto an OS thread and the kernel schedules it. Java 21 added virtual threads, which the JVM schedules and multiplexes onto a much smaller pool of carrier threads.
When would you choose a separate process instead of a thread?+
When isolation matters more than speed: untrusted code, a crash that must not take the host down, a component you want to kill and restart. You pay for it with IPC and a heavier context switch, and you get a real safety boundary in return.