Skip to content
notes.vinhnq.com

Search

ConcurrencyJunior15 min read

Threads in Java: creating threads, the lifecycle and interruption

Review 10 cards
On this page

Threads and concurrency in Java series: 1. Creating threads and the lifecycle · 2. Race conditions, volatile, deadlocks · 3. ExecutorService and virtual threads

Tomcat, HikariCP, @Scheduled and Kafka consumers all run on threads; when a counter drifts, the app freezes or user A sees user B’s data, the person who understands threads is the one who finds the cause. After this post you can create threads with Thread/Runnable/Callable, read the six thread states and stop a thread properly with interrupt. You need basic Java and exceptions; part 2 covers race conditions, volatile and deadlocks, part 3 thread pools, virtual threads and ThreadLocal (demos run on JDK 21).

1. Processes, threads, concurrency and parallelism

Key idea: a process is a running program with its own memory; a thread is a path of execution inside a process that shares the heap and is the unit the OS schedules onto a CPU.

Each thread has its own stack (local variables, method frames) and its own program counter. How the OS manages processes, threads and context switches (moving a CPU from one thread to another): see Operating systems for backend engineers: kernel, system calls, processes and threads.

Process Thread
Memory Private, isolated Shared heap, private stack
Creation cost High (fork, new page tables) Much lower, but still ~1 MB of stack plus an OS thread
Communication IPC: sockets, pipes, files Reading and writing shared objects on the heap
When one dies Other processes are fine An uncaught exception only kills that thread, but corrupted shared data hurts the whole process

“Listening to music while browsing and downloading a file” is really three processes. A proper thread example: inside one JVM, each HTTP request is handled by one of Tomcat’s threads.

  • Concurrency: several tasks in progress at once, possibly interleaved on one core. It’s about structure.
  • Parallelism: several tasks actually running at the same instant on multiple cores. It’s about execution.

A two-core server handling 200 requests at once: high concurrency, parallelism at most 2.

When threads actually help

  • I/O-bound (database, HTTP, file reads): threads mostly wait, so more threads keep the CPU busy. The common backend case.
  • CPU-bound (image compression, encryption): threads are always busy; more threads than cores isn’t faster, it just adds context switches.
  • Responsiveness: keep slow work (sending email, exporting a report) off threads that must stay free, such as the request thread or a Netty event loop.

The price: race conditions (shared data gets corrupted), deadlocks (waiting on each other forever), and timing-dependent bugs that are hard to reproduce; part 2 revolves around those three.

More thread examples and the threads the JVM starts
  • A browser example: inside one browser, one thread paints the UI, one downloads images, one runs JavaScript.
  • Responsiveness outside the backend: the UI thread of a desktop or Android app must always be free, so slow work runs on another thread.
  • Every Java program starts with at least the main thread, which runs the main method, plus a few JVM background threads (GC, JIT compiler, Finalizer, Signal Dispatcher).

2. Creating threads: Thread, Runnable, Callable

Key idea: separate “the work” (Runnable, or Callable when you need a result) from “the worker” (Thread or an executor), and remember only start() actually creates a new thread.

extends Thread and implements Runnable

// Option 1: subclass Thread
class ReportThread extends Thread {
    @Override
    public void run() {
        System.out.println("building report on " + getName());
    }
}

// Option 2: separate "the work" (Runnable) from "the worker" (Thread)
Runnable sendEmail = () -> System.out.println("sending on " + Thread.currentThread().getName());

new ReportThread().start();
new Thread(sendEmail, "mailer").start();

Runnable is a functional interface with a single void run(), so you can write it as a lambda.

Two threads interleaving

The steps: a class that implements Runnable, override run(), pass an instance to a Thread constructor, call start(). The demo below shows:

  • main prints its line before either thread, because start() returns immediately instead of waiting for run().
  • The order between the two threads is up to the OS scheduler (resize 3 came before download 3 even though download started first). Don’t rely on it; to wait for a thread, use join() (section 4).
Demo: two interleaving countdown threads and their output

Two countdowns on two threads, each pausing with Thread.sleep so you can see them interleave:

public class Interleave {
    static class Countdown implements Runnable {
        private final String label;
        private final long pauseMs;

        Countdown(String label, long pauseMs) {
            this.label = label;
            this.pauseMs = pauseMs;
        }

        @Override
        public void run() {
            for (int i = 3; i >= 1; i--) {
                System.out.println(label + " " + i + " on " + Thread.currentThread().getName());
                try {
                    Thread.sleep(pauseMs);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread download = new Thread(new Countdown("download", 100), "t-download");
        Thread resize = new Thread(new Countdown("resize", 150), "t-resize");
        download.start();
        resize.start();
        System.out.println("main keeps going on " + Thread.currentThread().getName());
        download.join();
        resize.join();
        System.out.println("both done");
    }
}

One run:

main keeps going on main
resize 3 on t-resize
download 3 on t-download
download 2 on t-download
resize 2 on t-resize
download 1 on t-download
resize 1 on t-resize
both done

Callable and Future: when you need a result

Runnable.run() returns nothing and can’t throw checked exceptions. Callable<V>.call() returns a V and may throws Exception. You can’t pass a Callable to new Thread(...); hand it to an ExecutorService (part 3) to get a Future<V>:

import java.util.concurrent.*;

public class CallableDemo {
    public static void main(String[] args) throws InterruptedException {
        try (ExecutorService pool = Executors.newFixedThreadPool(2)) {
            Callable<Integer> priceLookup = () -> {
                Thread.sleep(200);              // simulate an API call
                return 42_000;
            };
            Future<Integer> price = pool.submit(priceLookup);
            Future<Integer> broken = pool.submit(() -> {
                throw new IllegalStateException("upstream down");
            });

            try {
                System.out.println("price = " + price.get(1, TimeUnit.SECONDS));
                broken.get();
            } catch (ExecutionException e) {
                System.out.println("task failed: " + e.getCause());
            } catch (TimeoutException e) {
                price.cancel(true);
                System.out.println("too slow, cancelled");
            }
        }
    }
}
// price = 42000
// task failed: java.lang.IllegalStateException: upstream down
  • get() blocks until the result is ready; in production always use the timeout overload.
  • An exception in the task is wrapped in ExecutionException; the original is in getCause().
  • cancel(true) interrupts the thread running the task (it only really stops if the task respects interruption, section 4).
  • To chain async steps, use CompletableFuture; a plain Future only has a blocking get().

start() vs run()

public class StartVsRun {
    public static void main(String[] args) throws InterruptedException {
        Runnable task = () -> System.out.println("running in: " + Thread.currentThread().getName());

        Thread t = new Thread(task, "worker-1");
        t.run();    // just a regular method call
        t.start();  // the JVM creates a new thread, which calls run()
        t.join();

        try {
            t.start(); // second time
        } catch (IllegalThreadStateException e) {
            System.out.println("start twice -> " + e.getClass().getSimpleName());
        }
    }
}
// running in: main
// running in: worker-1
// start twice -> IllegalThreadStateException

3. Lifecycle: the six thread states

Key idea: Thread.getState() returns one of six values; the interview favourites are that there is no RUNNING, I/O still shows RUNNABLE, and BLOCKED is only for synchronized.

State Meaning How a thread gets there
NEW Object created, not started yet new Thread(...)
RUNNABLE Running or ready to run, waiting for the OS to give it a CPU start()
BLOCKED Waiting to enter a synchronized block whose monitor another thread holds Contended synchronized
WAITING Waiting indefinitely until another thread wakes it up wait(), join(), LockSupport.park() (which is also how ReentrantLock.lock() waits)
TIMED_WAITING Like WAITING, but with a time limit sleep(ms), wait(ms), join(ms), parkNanos
TERMINATED run() has finished (normally or with an exception) End of run()
stateDiagram-v2
  [*] --> NEW
  NEW --> RUNNABLE: start
  RUNNABLE --> BLOCKED: enter a held synchronized block
  BLOCKED --> RUNNABLE: monitor acquired
  RUNNABLE --> WAITING: wait / join / park
  WAITING --> RUNNABLE: notify / other thread ends / unpark
  RUNNABLE --> TIMED_WAITING: sleep / timed wait / timed join
  TIMED_WAITING --> RUNNABLE: timeout / woken up
  RUNNABLE --> TERMINATED: run returns
  TERMINATED --> [*]
  • Java has no separate “RUNNING” state: running on a CPU and queued for one are both RUNNABLE.
  • A thread blocked on I/O (reading a socket or file) still reports RUNNABLE, because to the JVM it’s in native code. In a thread dump, don’t assume “RUNNABLE = burning CPU”.
  • BLOCKED is only for synchronized. A thread waiting on a ReentrantLock shows WAITING (the lock uses LockSupport.park).
Demo producing all six states, and the state-reading trap
public class ThreadStates {
    private static final Object LOCK = new Object();

    public static void main(String[] args) throws Exception {
        Thread sleeper = new Thread(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
        System.out.println("1. " + sleeper.getState());          // NEW
        sleeper.start();
        Thread.sleep(100);
        System.out.println("2. " + sleeper.getState());          // TIMED_WAITING

        Thread blocked = new Thread(() -> {
            synchronized (LOCK) { /* needs LOCK, which main is holding */ }
        });
        Thread waiter = new Thread(() -> {
            try {
                sleeper.join();                                    // waits with no time limit
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        });
        synchronized (LOCK) {
            blocked.start();
            waiter.start();
            Thread.sleep(100);
            System.out.println("3. " + blocked.getState());      // BLOCKED
            System.out.println("4. " + waiter.getState());       // WAITING
        }

        Thread spinner = new Thread(() -> {
            long end = System.nanoTime() + 300_000_000L;
            while (System.nanoTime() < end) { /* busy computing */ }
        });
        spinner.start();
        Thread.sleep(50);
        System.out.println("5. " + spinner.getState());          // RUNNABLE

        sleeper.join();
        waiter.join();
        blocked.join();
        spinner.join();
        System.out.println("6. " + sleeper.getState());          // TERMINATED
    }
}

4. Controlling threads: sleep, join, interrupt

Key idea: sleep pauses the current thread, join waits for another to finish; the only way to stop a thread is interrupt(), and the other thread has to cooperate.

  • Thread.sleep(ms): does not release any locks it holds.
  • t.join() / t.join(ms): besides waiting, guarantees that everything t wrote is visible once join returns (happens-before, see part 2).
  • obj.wait() / obj.notifyAll(): low-level wait/wake-up; you must hold obj’s monitor, and wait() releases it while waiting. New code uses BlockingQueue, CountDownLatch, CompletableFuture.

Interruption: the only polite way to stop a thread

Java has no safe way to “kill” a thread: Thread.stop() has been disabled for good because it could abort mid-update and corrupt data. Stopping is cooperative: A calls b.interrupt() to set B’s interrupt flag; B checks the flag and stops itself.

What B is doing What happens
Blocked in sleep, wait, join, BlockingQueue.take/poll… The method throws InterruptedException and clears the flag
Computing Nothing, until B checks Thread.currentThread().isInterrupted() itself
Classic socket I/O (InputStream.read) Ignores it; close the socket or set a timeout

A worker that does it right:

import java.util.concurrent.*;

public class Interruption {
    static void pollQueue(BlockingQueue<String> queue) {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                String job = queue.poll(1, TimeUnit.SECONDS);
                if (job != null) System.out.println("processing " + job);
            } catch (InterruptedException e) {
                // sleep/poll/wait clear the interrupt flag when they throw -> set it again
                Thread.currentThread().interrupt();
                System.out.println("interrupted, cleaning up");
            }
        }
        System.out.println("worker exits, flag = " + Thread.currentThread().isInterrupted());
    }

    public static void main(String[] args) throws Exception {
        BlockingQueue<String> queue = new LinkedBlockingQueue<>();
        Thread worker = new Thread(() -> pollQueue(queue), "poller");
        worker.start();
        queue.put("job-1");
        Thread.sleep(200);
        worker.interrupt();
        worker.join(2000);
        System.out.println("worker state = " + worker.getState());
    }
}
// processing job-1
// interrupted, cleaning up
// worker exits, flag = true
// worker state = TERMINATED

Exercises

Exercise 1: Predict the output and the states

The task (paraphrased): some code creates a thread, calls run(), then start(), then start() again after join(), printing the thread name inside the task and the thread state at each step. Predict the output.

Solution to Exercise 1

This is the StartVsRun demo from section 2. run() prints main; start() prints worker-1; the second start() throws IllegalThreadStateException because the thread is TERMINATED and a Thread can only be started once. States: NEW before start(), TERMINATED after join(); right after start() you can’t predict it reliably (see the pitfall in section 3).

Remember

  • Explain how a process and a thread differ in memory, creation cost and communication.
  • Tell concurrency from parallelism, and know when adding threads is pointless.
  • Say why run() creates no new thread and why a second start() throws.
  • List the six states and why a thread waiting on I/O is still RUNNABLE.
  • Know the two correct ways to handle InterruptedException.

Interview questions

Junior

What’s the difference between a process and a thread?

A process is a running program with its own memory; a thread is a path of execution inside a process that shares the heap but has its own stack.

The thread is the unit the OS schedules. Threads are cheaper and communicate faster (by sharing objects), but because they share memory they need careful synchronization.

What’s the difference between start() and run()?

start() asks the JVM to create a new thread and call run() on it; calling run() directly is just a method call on the current thread.

Calling run() gives no concurrency. start() can only be called once per Thread object; a second call throws IllegalThreadStateException.

How do Runnable and Callable differ, and why prefer them over extends Thread?

Callable returns a result and may throw checked exceptions, Runnable can’t; both separate the work from the thread that runs it.

A Callable<V> is submitted via ExecutorService.submit to get a Future<V>. Being separate from the thread, both work with thread pools and virtual threads and don’t use up your single superclass, unlike extends Thread.

What states can a Java thread be in?

Six: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED; there’s no separate RUNNING state.

NEW is not started; RUNNABLE is running or waiting for a CPU, including blocked on I/O; BLOCKED is waiting to enter synchronized; WAITING is wait, join, park with no timeout; TIMED_WAITING is sleep and the timed variants; TERMINATED is finished.

Mid

How does sleep() differ from wait()?

sleep keeps its locks and can be called anywhere; wait must hold the monitor and releases it while waiting.

Thread.sleep is static and pauses the current thread. obj.wait() without holding obj’s monitor throws IllegalMonitorStateException; it waits for notify/notifyAll or a timeout, and must sit in a while (!condition) loop because of spurious wakeups. New code should use BlockingQueue, CountDownLatch or Condition.

How should you handle InterruptedException?

Either rethrow it, or call Thread.currentThread().interrupt() to restore the flag and stop what you’re doing.

The blocking method cleared the flag when it threw. Swallowing it with an empty catch or just a log line means shutdownNow() and Future.cancel(true) can’t stop the task.

Useful phrases for interviews

  • “Concurrency is about structuring a program to deal with many things at once; parallelism is about actually running them at the same time on several cores.”
  • “Interruption is cooperative: if I can’t rethrow InterruptedException, I restore the interrupt flag so the caller still knows.”
  • “A process has its own memory; the threads inside it share the heap, but each one has its own stack.”
  • “Calling run() directly just runs the code on the current thread; only start() creates a new one.”
  • “I write tasks as a Runnable or a Callable and let an executor decide which thread runs them.”
  • “There’s no separate RUNNING state in Java; a thread blocked on socket I/O still shows up as RUNNABLE.”
  • “sleep keeps whatever locks it holds, while wait releases the monitor until it’s notified.”
  • “I’d never coordinate threads in a test with Thread.sleep; a CountDownLatch or join states the intent.”

Notes based on the Hỏi Dân IT Java Core material plus my own research, rewritten in my own words.