Thread vs Process in Java
Fast recall
Quick revision
Core answer
Fundamental understanding of concurrency concepts and memory model distinctions
Saying threads are completely isolated — they share heap memory, which is both their power and risk
Explain shared heap as the reason for race conditions, and the context switch cost difference
Interview-ready guide
Interview answer
A process is a running program with its own address space and operating-system resources. A thread is an execution path within that process. In one Java process, threads can reach the same heap objects and static state, but each thread has its own call stack.
This is why threads can communicate through shared objects without serializing every value, and also why a shared mutable object can race. Separate processes are isolated by default and normally communicate through IPC such as sockets or pipes. Threads are commonly lighter to create and coordinate, but I would not claim a thread switch saves only registers while a process switch swaps all memory; those costs depend on the JVM and operating system.
Thread vs Process in Java
Direct Answer
A process is a running program with its own address space and operating-system resources. A thread is an execution path inside a process. Threads in one Java process share heap objects and class state, while each has its own call stack. Sharing makes communication convenient but requires safe coordination for mutable data.
Code Example
public class SharedStateDemo {
private static int shared; // class state reachable by both threads
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> {
for (int i = 0; i < 1_000; i++) {
shared++; // unsafe compound update
}
};
Thread first = new Thread(task);
Thread second = new Thread(task);
first.start();
second.start();
first.join();
second.join();
System.out.println(shared); // 2,000 is not guaranteed
}
}
Both threads can reach the same class state because they run in one process. A separate process cannot access this Java variable directly; cross-process communication needs an operating-system or external mechanism, which can include explicitly shared memory.
Common Mistakes & Pitfalls
- Treating threads like isolated processes: threads in one process can reach the same heap objects and class state.
- Saying each process switch copies all memory: modern virtual-memory systems do not require that model.
- Saying processes can never share memory: they can use explicit shared-memory facilities, though ordinary Java objects are not directly shared across processes.
- Quoting one fixed cost: creation and switching vary by platform and thread type.
- Assuming more threads means more CPU: parallel computation is still limited by available processors.
Check your understanding
Answer these without looking back at the article.
- What is a race condition?
- Why are threads lighter than processes?
- What is context switching and why does it matter?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.