What is the difference between a process and a thread in Java?
A process and a thread are both units of execution, but they operate at different levels of the operating system and have fundamentally different resource isolation models.
A **process** is an instance of a running program. The OS allocates it its own private virtual memory address space, file handles, network connections, and system resources. Processes are isolated from each other — a crash in one process does not directly affect others. Communication between processes requires inter-process communication (IPC) mechanisms: pipes, sockets, shared memory, message queues. Creating a process is relatively expensive; the OS must allocate and initialize a new address space.
A **thread** is a unit of execution within a process. All threads in the same process share the process's memory space — the heap, static fields, and open resources. Each thread has its own program counter, stack, and register state, but nothing else is isolated. This shared memory makes communication between threads fast (just read/write shared variables), but requires careful synchronization to avoid data races and memory visibility issues.
In the JVM context: when you launch a Java application, the JVM itself runs as a single OS process. Inside that process, the JVM creates multiple threads: the main thread, GC threads, JIT compiler threads, finalizer thread, and any application threads you create.
Because threads share heap memory, concurrent access to shared objects without synchronization is a bug — data races produce undefined, non-deterministic behavior. The Java Memory Model (JMM) defines the rules for when one thread's writes are visible to another: `synchronized` blocks, `volatile` fields, and `java.util.concurrent` primitives all establish happens-before relationships.
Context switching between threads in the same process is cheaper than switching between processes because there is no address space change, but it is not free — the OS still saves and restores register state.
| Aspect | Process | Thread |
|---|---|---|
| Memory | Own private address space | Shares process heap with other threads |
| Isolation | High — crash doesn't affect other processes | Low — a thread crash can kill the process |
| Communication | IPC (sockets, pipes, shared memory) | Shared memory (direct variable access) |
| Creation cost | High — new address space | Lower — new stack and registers only |
| Context switch | Expensive (address space change) | Cheaper (same address space) |
| Synchronization needed? | Not for private data | Yes — shared heap requires coordination |
Correctly explains memory isolation (process own space vs thread shared heap) and communication differences.
Also covers context switch cost, the JVM as a process with multiple internal threads, the Java Memory Model's role in thread visibility, and happens-before relationships.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment