Home/Learn/Operating Systems/Process vs Thread

Process vs Thread

Beginner
Processes & Threads

A process is an independent program in execution with its own memory space, while a thread is a lightweight unit of execution that shares memory within the same process.

Overview

A process is like a browser tab — each has its own heap, stack, code segment, and OS resources. Threads within a process share the heap and code segment but each maintain their own stack and program counter. Creating a process is expensive (requires duplicating the address space via fork()), whereas creating a thread is cheap. Java models this directly: each JVM instance is a process, and java.lang.Thread represents a thread within that process. Modern applications use threads to achieve concurrency without the overhead of multiple processes, but thread safety becomes the programmer's responsibility.

Process vs Thread: Key Differences

Processes are isolated — a crash in one process does not affect others. Threads share memory, making communication fast but introducing race conditions. Context-switching between processes is slower than switching between threads because the OS must swap out the entire memory map.

Java — ProcessBuilder vs Thread
// Process: launching a separate OS process from Java
ProcessBuilder pb = new ProcessBuilder("python3", "script.py");
pb.redirectErrorStream(true);
Process process = pb.start();
int exitCode = process.waitFor();  // blocks until child process finishes
System.out.println("Exit: " + exitCode);

// Thread: lightweight execution unit within the same JVM process
Thread thread = new Thread(() -> {
    System.out.println("Thread id: " + Thread.currentThread().getId());
    // shares heap with parent — can read/write same objects
});
thread.start();
thread.join();  // wait for thread to finish

Memory Layout Comparison

Each process has its own virtual address space: code, data, heap, and stack segments. Threads share code, data, and heap but have independent stacks. This is why a stack overflow in one thread can kill the JVM (shared process), while a crashed child process leaves the parent intact.

Java — shared heap, separate stacks
// Demonstrating shared heap between threads
class SharedCounter {
    int count = 0;  // lives on heap — visible to ALL threads
}

SharedCounter counter = new SharedCounter();

Thread t1 = new Thread(() -> counter.count++);
Thread t2 = new Thread(() -> counter.count++);
t1.start(); t2.start();
t1.join();  t2.join();
// count may be 1 (race condition!) not 2
// Fix: use AtomicInteger or synchronized

// Each thread has its own stack:
Thread t3 = new Thread(() -> {
    int localVar = 42;  // on t3's stack — NOT shared
    System.out.println(localVar);
});

Interview Angle

Interviewers at Amazon and Google often ask this as a warm-up before diving into synchronization. Know that Java's Thread maps to a kernel-level OS thread (one-to-one model on the JVM), and that virtual threads (Project Loom, Java 21) are lightweight user-mode threads managed by the JVM scheduler.

Java 21 — virtual threads vs platform threads
// Java 21 Virtual Threads (Project Loom) — millions of threads cheaply
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    for (int i = 0; i < 1_000_000; i++) {
        executor.submit(() -> {
            Thread.sleep(Duration.ofMillis(100));  // parks, not blocks OS thread
            return "done";
        });
    }
}  // auto-shutdown

// Check if current thread is virtual
System.out.println(Thread.currentThread().isVirtual());

Key Points to Remember

  • 1A process has its own memory space; threads within a process share the heap.
  • 2Process creation (fork) is expensive; thread creation is cheap.
  • 3A thread crash can kill the entire process; a process crash is isolated.
  • 4Java threads map 1-to-1 to OS kernel threads on the HotSpot JVM.
  • 5Java 21 virtual threads are user-space threads that do not block OS threads.
  • 6Inter-process communication (IPC) requires pipes/sockets; inter-thread communication uses shared memory.

Interview Questions

Sign in to ask Aria
1

What is the difference between a process and a thread?

EasyAmazon
2

Why is context switching between threads faster than between processes?

MediumGoogle
3

What memory do threads share and what do they keep private?

EasyMicrosoft
4

How do Java virtual threads differ from platform threads?

MediumNetflix
5

If a thread throws an uncaught exception, what happens to the process?

MediumFlipkart

Ask Aria about Process vs Thread

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…