Skip to main content
PreparationZone Logo
PrepZone

Threads and Their Lifecycle

Process versus thread, creating threads, the six states, and what daemon threads change about shutdown.

Pass checkpoint below

Read these first

Why this matters

  • The start() versus run() 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.

AspectProcessThread
Created byThe OS, via ProcessBuilder or a new JVMThe JVM, via new Thread or an executor
MemoryPrivate address spaceOwn stack and program counter; heap is shared
CrashTakes its threads with itAn uncaught exception kills that thread, not the process
CostHeavy: a new JVM is hundreds of millisecondsA platform thread is a stack plus an OS thread
CommunicationPipes, sockets, filesShared objects, with synchronization
  • Created by

    ProcessThe OS, via ProcessBuilder or a new JVM
    ThreadThe JVM, via new Thread or an executor
  • Memory

    ProcessPrivate address space
    ThreadOwn stack and program counter; heap is shared
  • Crash

    ProcessTakes its threads with it
    ThreadAn uncaught exception kills that thread, not the process
  • Cost

    ProcessHeavy: a new JVM is hundreds of milliseconds
    ThreadA platform thread is a stack plus an OS thread
  • Communication

    ProcessPipes, sockets, files
    ThreadShared 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

Java
Loading compiler…
Aspectextends Threadimplements Runnable
InheritanceUses the single extends slotLeaves it free
SeparationTask and thread are one objectThe task is independent of how it runs
ReusableA Thread cannot be restartedThe same Runnable can be submitted many times
Works with executorsAwkwardlyDirectly
RecommendedNoYes
  • Inheritance

    extends ThreadUses the single extends slot
    implements RunnableLeaves it free
  • Separation

    extends ThreadTask and thread are one object
    implements RunnableThe task is independent of how it runs
  • Reusable

    extends ThreadA Thread cannot be restarted
    implements RunnableThe same Runnable can be submitted many times
  • Works with executors

    extends ThreadAwkwardly
    implements RunnableDirectly
  • Recommended

    extends ThreadNo
    implements RunnableYes

Prefer Runnable. In practice, prefer an executor over creating threads at all.

The six states

NEWCreated, start() not yet called
RUNNABLEEligible for the CPU
BLOCKEDWaiting for a monitor lock
WAITINGwait() / join()
TIMED_WAITINGsleep(ms)
TERMINATEDrun() returned
A thread only runs in RUNNABLE. Blocked, waiting and timed-waiting are three different reasons for not running, and knowing which one a thread is in tells you where to look.
  • 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.
Java
Loading compiler…

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.