Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Multithreading

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Multithreading

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
Thread states in order: NEW after new Thread, RUNNABLE after start, TIMED_WAITING during sleep(500), and TERMINATED when run ends. Meanwhile main is WAITING at courier.join() until the courier finishes. NEWRUNNABLETIMED_WAITINGTERMINATED new Thread(...)start()sleep(500)run() ends main: WAITING at courier.join()
One courier thread walking through its states while main waits at the join gate.

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

CallWhat 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.

Keep going in Java · 67 of 79

Previous
Creating Threads in Java: Thread, Runnable and Lambdas
Next post
Java Race Conditions and synchronized
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • Ask a thread what state it is in
  • interrupt() wakes a sleeping thread
  • The same interrupt, but the code stops
  • A thread that ends with the program
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.