← Back to Java

Process vs Thread in Java

JavaMidjava

The Question

What is the difference between a process and a thread in Java?

What a Strong Answer Covers

  • process = independent memory space
  • thread = lightweight shares process memory
  • threads share heap
  • each thread has own stack
  • faster context switching for threads
  • thread crash affects process

Senior-Level Answer

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.

Key Differences

AspectProcessThread
MemoryOwn private address spaceShares process heap with other threads
IsolationHigh — crash doesn't affect other processesLow — a thread crash can kill the process
CommunicationIPC (sockets, pipes, shared memory)Shared memory (direct variable access)
Creation costHigh — new address spaceLower — new stack and registers only
Context switchExpensive (address space change)Cheaper (same address space)
Synchronization needed?Not for private dataYes — shared heap requires coordination

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly explains memory isolation (process own space vs thread shared heap) and communication differences.

3/3 — Strong Answer

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.

Common Mistakes

  • Saying threads are completely independent — they share heap memory and a crash in one thread (uncaught exception) terminates the entire process.
  • Thinking thread communication is free — shared memory is fast but requires synchronization; unsynchronized access causes data races.
  • Not knowing about the JVM's own internal threads (GC, JIT, finalizer) beyond application threads.

Follow-Up Questions

  • What is a data race and why is it a problem even without an obvious crash? — Two threads access shared memory concurrently without synchronization; the Java Memory Model permits the JIT to reorder and cache values, producing non-deterministic results.
  • How does the JVM implement Java threads relative to OS threads? — Java threads map 1:1 to OS threads in modern JVMs (HotSpot). Java 21 Virtual Threads (Project Loom) add M:N scheduling on carrier threads.
  • What happens to the JVM process when a non-daemon thread throws an uncaught exception? — The thread terminates; if it's the last non-daemon thread, the JVM exits. Daemon threads are killed without running finally blocks.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

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
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout