Java Thread Lifecycle: sleep, join and interrupt
A courier thread walks from NEW through RUNNABLE and TIMED_WAITING to TERMINATED while main WAITs at join. An interrupt wakes a sleeping download on report-3: one version retries it, the other returns. And a daemon status thread dies when the last user thread ends.
What you will learn
- Read a thread's state with getState: NEW, RUNNABLE, TIMED_WAITING, WAITING, TERMINATED
- Pause a thread with sleep and wait for another with join
- Explain that interrupt is a signal, and let the catch block resume or stop
- Make a background thread a daemon so it ends with the program
Before you start
- Creating threads in Java: Thread, Runnable and lambdas
- Java exception handling: try, catch and finally
A thread has a life: it's created, runs, waits, and finishes. This lesson shows each state, then three ways to steer a thread: sleep, join and interrupt, plus daemon threads. Where threads print side by side, the order of lines and timings can differ from run to run.
Ask a thread what state it is in
package org.studyeasy.s06l03;
public class Ex01_ThreadStates {
static void show(String who, Thread t) {
System.out.println(who + ": " + t.getState());
}
public static void main(String[] args) throws InterruptedException {
Thread main = Thread.currentThread(); // main asks about itself
Thread courier = new Thread(() -> {
try {
Thread.sleep(500); // in transit: pauses THIS thread
show("main, while it joins", main);
} catch (InterruptedException e) {
System.out.println("Delivery cancelled");
}
}, "courier");
show("courier before start", courier);
courier.start();
show("main right now", main);
Thread.sleep(100); // let the courier reach sleep()
show("courier in transit", courier);
courier.join(); // main waits for the courier
show("courier after join", courier);
}
}
courier before start: NEW
main right now: RUNNABLE
courier in transit: TIMED_WAITING
main, while it joins: WAITING
courier after join: TERMINATED
A new Thread object is NEW: created, not started. start() makes it RUNNABLE, ready to run. sleep(500) pauses the thread that calls it, so the courier is TIMED_WAITING. When main reaches join(), it stops at the gate: main is WAITING (no time limit), and the courier itself prints that. When run ends, the courier is TERMINATED, the gate lifts and main carries on. A terminated thread can't be started again.
interrupt() wakes a sleeping thread
Thread download = new Thread(() -> {
int file = 1;
while (file <= 4) {
System.out.println("Downloading report-" + file);
try {
Thread.sleep(300); // slow network
file++; // only a finished file moves on
} catch (InterruptedException e) {
System.out.println("Interrupted on report-" + file);
}
}
System.out.println("All 4 reports downloaded");
}, "download");
download.start();
Thread.sleep(750); // the network drops in report-3
System.out.println("main: network dropped, interrupt!");
download.interrupt(); // wakes the sleep with an exception
download.join();
Downloading report-1
Downloading report-2
Downloading report-3
main: network dropped, interrupt!
Interrupted on report-3
Downloading report-3
Downloading report-4
All 4 reports downloaded
Each report takes 300 ms, so at 750 ms the download is asleep in report 3. interrupt() doesn't kill the thread. It's a signal: the sleep ends early with an InterruptedException (the checked exception sleep declares). This catch block just prints a line, and because file++ only runs after a finished sleep, the loop tries report 3 again and finishes all four.
The same interrupt, but the code stops
for (int file = 1; file <= 4; file++) {
System.out.println("Downloading report-" + file);
try {
Thread.sleep(300); // slow network
} catch (InterruptedException e) {
System.out.println("Interrupted on report-" + file);
return; // our own code decides to stop
}
}
Downloading report-1
Downloading report-2
Downloading report-3
main: network dropped, interrupt!
Interrupted on report-3
download is now TERMINATED
Same signal, different choice. The return in the catch ends the job, so the thread is TERMINATED and "All 4 reports downloaded" never prints. Your catch block decides what an interrupt means.
A thread that ends with the program
Thread status = new Thread(() -> {
while (true) { // never ends on its own
System.out.println("status: still working");
pause(400);
}
}, "status");
status.setDaemon(true); // dies when the last user thread ends
export.start();
status.start();
System.out.println("main started both, main ends");
main started both, main ends
status: still working
status: still working
export part 1 of 3 saved
status: still working
export part 2 of 3 saved
status: still working
export part 3 of 3 saved
That's one real run; your order may differ. export is a normal (user) thread that saves three parts, 500 ms apart. status loops forever. Main ends at once, but the program keeps running while a user thread is alive. When export saves part 3, it's the last user thread, so the program ends and the daemon status thread is stopped with it. Delete setDaemon(true) and status keeps printing; the program never ends.
Recap
| Call | What it does |
|---|---|
t.getState() | NEW, RUNNABLE, TIMED_WAITING, WAITING, TERMINATED |
Thread.sleep(ms) | pauses the thread that calls it |
t.join() | waits for t to finish |
t.interrupt() | a signal, not a kill; the catch block decides |
t.setDaemon(true) | t ends when the last user thread ends |
Next: what goes wrong when threads share data, in race conditions and synchronized.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Concurrency: Processes, Threads and the Main Thread in Java
Every Java program runs as a process and starts on one thread, main. One cook takes 15 seconds for soup and salad; a second thread, Jack, brings it to 10. Then two threads add to one counter a million times each and count less than 2,000,000.
Creating Threads in Java: Thread, Runnable and Lambdas
Three ways to give a thread its job: an InvoiceMailer that extends Thread, a ReportTask record that implements Runnable, and a lambda. worker.run() prints main, worker.start() prints backup-worker, and two cashiers, Jack and Liam, scan items in an order that changes every run.
Java Race Conditions and synchronized
Two cashiers each sell a million tickets on one counter and the total comes out short, a different number every run, because sold++ is read, add, write. A synchronized method or a synchronized block on one shared lock gives exactly 2,000,000; a new Object() lock protects nothing.
Java volatile and Atomic Variables
main sets a volatile running flag to false and the sync job finishes batch 3, sees the change and exits. But volatile can't make views++ one step, so two threads still lose updates. An AtomicInteger with incrementAndGet counts web and mobile visits to exactly 200,000.
