Why this matters
- The
start()versusrun()distinction is the most common beginner mistake in Java concurrency, and it fails silently — the code runs, just not concurrently. - The six thread states are the vocabulary of every thread dump you will ever read while diagnosing a hang.
- Daemon threads decide whether your JVM exits, which is a frequent cause of a process that will not shut down.
Process and thread
A process is the operating system's isolation boundary. The JVM is one process: its own address space, its own heap, its own file handles. A thread is a path of execution inside that process.
| Aspect | Process | Thread |
|---|---|---|
| Created by | The OS, via ProcessBuilder or a new JVM | The JVM, via new Thread or an executor |
| Memory | Private address space | Own stack and program counter; heap is shared |
| Crash | Takes its threads with it | An uncaught exception kills that thread, not the process |
| Cost | Heavy: a new JVM is hundreds of milliseconds | A platform thread is a stack plus an OS thread |
| Communication | Pipes, sockets, files | Shared objects, with synchronization |
Created by
ProcessThe OS, via ProcessBuilder or a new JVMThreadThe JVM, via new Thread or an executorMemory
ProcessPrivate address spaceThreadOwn stack and program counter; heap is sharedCrash
ProcessTakes its threads with itThreadAn uncaught exception kills that thread, not the processCost
ProcessHeavy: a new JVM is hundreds of millisecondsThreadA platform thread is a stack plus an OS threadCommunication
ProcessPipes, sockets, filesThreadShared objects, with synchronization
Threads in one JVM share the heap and metaspace. They do not share stacks.
Sharing the heap is why two threads can see the same ArrayList and also why that is unsafe without a memory-model guarantee. Starting a second process does not share objects at all; you pay for isolation with serialization and a much slower start. Virtual threads, covered later, are still threads inside this one process — they share the heap too.
Three ways to define work
| Aspect | extends Thread | implements Runnable |
|---|---|---|
| Inheritance | Uses the single extends slot | Leaves it free |
| Separation | Task and thread are one object | The task is independent of how it runs |
| Reusable | A Thread cannot be restarted | The same Runnable can be submitted many times |
| Works with executors | Awkwardly | Directly |
| Recommended | No | Yes |
Inheritance
extends ThreadUses the single extends slotimplements RunnableLeaves it freeSeparation
extends ThreadTask and thread are one objectimplements RunnableThe task is independent of how it runsReusable
extends ThreadA Thread cannot be restartedimplements RunnableThe same Runnable can be submitted many timesWorks with executors
extends ThreadAwkwardlyimplements RunnableDirectlyRecommended
extends ThreadNoimplements RunnableYes
Prefer Runnable. In practice, prefer an executor over creating threads at all.
The six states
- NEW — constructed but
start()has not been called. - RUNNABLE — eligible to run. The thread may be executing or waiting for a CPU; Java does not distinguish.
- BLOCKED — waiting to acquire a monitor lock held by another thread.
- WAITING — waiting indefinitely for another thread to act:
wait(),join(),LockSupport.park(). - TIMED_WAITING — the same, with a deadline:
sleep(ms),wait(ms),join(ms),poll(timeout). - TERMINATED —
run()has completed, normally or by throwing.
These names are exactly what appears in a thread dump, which is why they are worth memorising:
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 6 sections
- Essential methods
- Interruption is cooperative
- Daemon threads
- Uncaught exceptions
- Prefer an executor
- Common misreadings
Test yourself
Answer these before moving on — recall is what makes it stick.