Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Thread class

Reference for java.lang.Thread: creating and starting threads, join, sleep, interrupt, daemon threads, virtual threads, names, priorities and common mistakes.

A Thread is one path of execution in a Java program. Every program starts with a main thread, and you create more to do work concurrently: waiting for I/O, handling requests, or using several CPU cores. Calling start() asks the JVM to run the thread's run() method in a new thread; calling run() directly just runs it in the caller's thread, which is a classic mistake.

Since Java 21 there are two kinds of threads: platform threads (a thin wrapper over an operating-system thread) and virtual threads, which are cheap enough to create by the million. Both are Thread objects.

Java
Thread t = new Thread(() -> System.out.println("hello from " + Thread.currentThread().getName()));
t.start();
t.join();

Try it

Thread output order is only deterministic when threads are joined, so these examples wait for each thread before printing the next step.

Java Starting and joining threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Interrupts, daemons and virtual threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class Thread implements Runnable
Member Description
Thread(Runnable), Thread(Runnable, String name) Creates a thread that will run the given task.
start() Begins execution. May be called once; a second call throws IllegalThreadStateException.
run() The task body. Override it in a subclass or pass a Runnable.
join() / join(Duration) Waits until the thread terminates. Throws InterruptedException.
static sleep(long millis) / sleep(Duration) Pauses the current thread.
interrupt(), isInterrupted(), static interrupted() Cooperative cancellation. interrupted() also clears the flag.
setDaemon(boolean), isDaemon() Daemon threads do not keep the JVM alive. Set before start().
getName(), setName(String), threadId() Identification (threadId() since Java 19).
getState() One of NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.
static currentThread() The calling thread.
static ofPlatform(), static ofVirtual() Builders (Java 21). startVirtualThread(Runnable) is a shortcut.
static yield(), static onSpinWait() Scheduling hints.
setUncaughtExceptionHandler(...) Handler for exceptions that escape run().

Usage notes

  • start(), not run(). Only start() creates concurrency.
  • Interrupt is a request. It sets a flag and wakes blocking calls (sleep, wait, join), which throw InterruptedException and clear the flag. Code that catches it must either stop or call Thread.currentThread().interrupt() to restore the flag.
  • Shared data needs synchronization. Threads reading and writing the same variable need synchronized, volatile, atomics or locks; otherwise results are unpredictable.
  • Uncaught exceptions kill only that thread. The stack trace is printed to System.err; the program keeps running.
  • Prefer executors for pools. Creating a platform thread per task is expensive. Use an ExecutorService, or Executors.newVirtualThreadPerTaskExecutor() for blocking I/O tasks.
  • Virtual threads (final in Java 21) are always daemon threads, ignore priorities and are ideal for many blocking tasks. They are less suited to long CPU-bound work, and holding a monitor (synchronized) while blocking could pin the carrier thread in older releases.
  • Priorities are hints and mostly ignored on modern operating systems.
  • Deprecated and dangerous: stop(), suspend() and resume(); since Java 20 stop() always throws UnsupportedOperationException.
  • Names help debugging. Name your threads; the default Thread-0 names tell nothing in a thread dump.