01 / 20

Threads vs.
Processes

Understand how operating systems manage execution units and how Java threads share memory within a single process.

Java

What is a Process?

A process is an independent program in execution. Every time you launch an application: a web browser, a game, or a Java program: the operating system creates a new process for it.

Own Memory
Each process gets its own private address space (heap, stack, code segment).
Isolated
One process cannot directly access or corrupt another process's memory.
Heavyweight
Creating a process is expensive: the OS must allocate memory, file handles, and security context.

What is a Thread?

A thread is the smallest unit of execution within a process. Think of it as a lightweight sub-task that runs inside the process's shared memory space.

Shared Memory
All threads within a process share the same heap memory and static variables.
Own Stack
Each thread has its own call stack and program counter to track where it is in execution.
Lightweight
Creating a thread is fast and cheap compared to spawning a new process.

Why Does This Matter?

Modern applications need to do many things at once: respond to user input, fetch data from the network, and render graphics. Understanding threads and processes is the foundation of concurrent programming in Java.

Process Memory Model

Process A Heap Memory Stack Code File Handles / Resources PID: 1024 Own address space Process B Heap Memory Stack Code File Handles / Resources PID: 2048 Own address space ✕ Isolated: no shared memory

Thread Memory Model

Single Process (JVM) Shared Heap Memory Objects, arrays, static fields: visible to ALL threads Thread 1 (main) Own Stack Program Counter Local Variables Thread 2 Own Stack Program Counter Local Variables Thread 3 Own Stack Program Counter Local Variables

The Core Rule

1
A process contains one or more threads. Every Java program starts with at least one thread (the main thread).
2
Threads within a process share heap memory. This makes communication fast, but introduces the risk of race conditions.
3
Processes have separate address spaces. One crashing process will not take down another process.

Key Distinction: Isolation vs. Sharing

Process Thread
Memory Separate address space Shared heap within process
Creation Cost Heavy: OS allocates full context Light: only stack + PC
Communication IPC (sockets, pipes, files) Shared variables (fast)
Crash Impact Only that process dies Can crash entire process
Example Chrome tabs (separate PIDs) Java's Thread class

How to Apply This Knowledge

1
Identify the task: Does it need its own isolated environment, or can it share data with other tasks?
2
Choose process if isolation matters: Security-sensitive tasks, plugin sandboxing, or unreliable external code.
3
Choose threads if performance matters: Parallel computation, responsive UI, or tasks that need to share data frequently.
4
If using threads, plan synchronization: Use synchronized, locks, or concurrent collections to prevent race conditions.
5
Test under concurrency: Bugs from shared state often only appear under load. Test with multiple threads active.

Example 1: Creating a Thread

public class MyThread extends Thread { public void run() { System.out.println("Hello from thread: " + Thread.currentThread().getName()); } public static void main(String[] args) { MyThread t1 = new MyThread(); t1.start(); // spawns a new thread System.out.println("Hello from main thread"); } }

Example 2: Threads Sharing Data

public class SharedCounter { static int count = 0; // shared across threads public static void main(String[] args) { Thread t1 = new Thread(() -> { for (int i = 0; i < 1000; i++) count++; }); Thread t2 = new Thread(() -> { for (int i = 0; i < 1000; i++) count++; }); t1.start(); t2.start(); } }

Example 3: Visualizing the Difference

Multi-Process Process A Memory A Thread 1 Resources A Process B Memory B Thread 1 Resources B ✕ No shared memory Multi-Thread (one process) Single Process Shared Heap Memory Thread 1 Stack PC Thread 2 Stack PC Thread 3 Stack PC Shared File Handles / Resources ✓ Shared memory: fast communication

Thread Lifecycle

NEW RUNNABLE RUNNING TERMINATED BLOCKED / WAITING start() scheduled run() ends wait/sleep notify Thread created not yet started

Launching a Process from Java

// Launch a separate OS process from Java ProcessBuilder pb = new ProcessBuilder( "python3", "script.py" ); Process process = pb.start(); // This is a completely separate process // It has its own memory, own PID int exitCode = process.waitFor(); System.out.println("Exited: " + exitCode);

Common Mistakes

✕ Calling run() instead of start()
Calling run() executes the code on the current thread. Only start() actually spawns a new thread.
✕ Thinking threads have separate memory
All threads in a process share the heap. Modifying a shared object from multiple threads without synchronization causes race conditions.
✕ Confusing thread with process
Saying "my program runs in a separate thread" when you mean a separate process (or vice versa) leads to incorrect design decisions.

Summary Comparison

Feature Process Thread
Address Space Own (isolated) Shared with process
Creation Speed Slow (heavy) Fast (lightweight)
Communication IPC: sockets, pipes Shared variables
Failure Impact Isolated crash Can crash whole process
Java API ProcessBuilder Thread, Runnable
Use When Isolation, security Parallelism, responsiveness

Question 1 of 5

Which of the following is true about threads within the same process?

Question 2 of 5

What happens when you call run() directly on a Thread object instead of start()?

Question 3 of 5

A Java application spawns a child process using ProcessBuilder. If the child process crashes, what happens to the parent Java application?

Question 4 of 5

Two threads simultaneously increment a shared static int counter variable 1000 times each without synchronization. What is the most likely outcome?

Question 5 of 5

Which scenario is best suited for using multiple threads rather than multiple processes?