Java Threads: Creating and Starting Them, and Why start() Is Not run()
Every way to create a Java thread, from extending Thread and implementing Runnable to lambdas and the Java 21 Thread.Builder. Why start() differs from run(), why executors replaced hand-made threads, and how to handle InterruptedException correctly.
kvi5hnuCode Sprout team member8 min readOn this page
There are only a few ways to create a thread in Java, and they have hardly changed since 1996. That stability is a strength: the same Thread and Runnable types now work for virtual threads too. This article covers the creation styles, the one mistake everyone makes once (run() instead of start()), the modern Thread.Builder API, and the right way to handle InterruptedException, which every blocking example in this course depends on.
Four ways to describe the work
A thread needs something to run. Java has offered four ways to supply it, each one shorter than the last:
| Style | Since | Example |
|---|---|---|
Subclass Thread and override run() | Java 1.0 | class Printer extends Thread { public void run() { ... } } |
Implement Runnable and pass it to Thread | Java 1.0 | new Thread(new PrintJob(), "printer") |
| Anonymous inner class | Java 1.1 | new Thread(new Runnable() { public void run() { ... } }) |
Lambda (because Runnable is a functional interface) | Java 8 | new Thread(() -> print(), "printer") |
Prefer Runnable (usually as a lambda) over subclassing Thread:
- It separates the task from the mechanism. A
Runnabledescribes what to do. The same object can be run on a platform thread, a virtual thread, a thread pool or the current thread, without changes. - It keeps your class's superclass free. Java has single inheritance, so extending
Threaduses up your oneextends. - It doesn't expose thread methods. A subclass of
Threadinheritsinterrupt(),setPriority()and the rest, which callers of your class have no business calling.
Subclassing Thread is only worth it when you really are customizing the thread itself, which is rare.
The modern way: Thread.Builder
Java 21 added builders, which make it easy to configure threads, especially their names:
Thread t = Thread.ofPlatform() // or Thread.ofVirtual()
.name("importer-", 1) // importer-1, importer-2, ...
.daemon(true)
.start(task); // or .unstarted(task) to start later
Thread.ofPlatform() and Thread.ofVirtual() return builders with the same basic methods, so switching between the two kinds of thread is a one-word change. A builder can also produce a ThreadFactory with .factory(), which is how you pass this configuration to an executor. Article 9 covers the virtual-thread builder in detail.
Name your threads. A thread dump with fifty threads called Thread-17 to Thread-66 tells you nothing. Names like importer-3 tell you what each thread was for.
start() creates a thread; run() does not
Calling start() asks the JVM to create a new thread of execution. The JVM allocates the thread's stack and other resources, registers it with the OS scheduler, and then calls run() on the new thread. start() itself returns immediately.
Calling run() yourself is just an ordinary method call. The task executes on the current thread, and no new thread is ever created.

A Thread object can be started only once. After it terminates, it stays in the TERMINATED state forever, and a second start() throws IllegalThreadStateException. To run the work again, create a new thread.
The following program shows all of this. Each task prints the name of the thread it actually ran on:
import java.util.ArrayList;
import java.util.List;
public class Solution {
static void report(String how) {
System.out.println(how + " -> ran on " + Thread.currentThread().getName());
}
static class PrinterThread extends Thread {
PrinterThread() { super("subclass-thread"); }
@Override public void run() { report("subclass of Thread"); }
}
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> report("Runnable passed to a Thread");
// 1. Calling run() directly: no new thread is created
new Thread(task, "never-started").run();
// 2. Subclass of Thread
Thread a = new PrinterThread();
a.start();
a.join();
// 3. Runnable handed to a Thread
Thread b = new Thread(task, "runnable-thread");
b.start();
b.join();
// 4. Thread.Builder (Java 21): named threads with a counter
Thread.Builder builder = Thread.ofPlatform().name("builder-", 1);
List<Thread> started = new ArrayList<>();
for (int i = 0; i < 2; i++) {
Thread t = builder.start(() -> report("Thread.Builder"));
t.join();
started.add(t);
}
// 5. start() twice is illegal
try {
a.start();
} catch (IllegalThreadStateException e) {
System.out.println("second start(): " + e.getClass().getSimpleName()
+ ", state was " + a.getState());
}
System.out.println("builder threads: " + started.stream().map(Thread::getName).toList());
}
}
Output:
Runnable passed to a Thread -> ran on main
subclass of Thread -> ran on subclass-thread
Runnable passed to a Thread -> ran on runnable-thread
Thread.Builder -> ran on builder-1
Thread.Builder -> ran on builder-2
second start(): IllegalThreadStateException, state was TERMINATED
builder threads: [builder-1, builder-2]
The first line is the trap: the thread was named never-started, but the work ran on main because nobody called start(). The program joins each thread before starting the next only to keep the output in a fixed order. In real code you start all the threads first, then join them.
join() waits for a thread to finish
t.join() blocks the calling thread until t terminates. It also guarantees visibility: everything t wrote to memory before it finished is visible to the thread that returns from join(). That's why the result of a worker can be read safely after joining it. join(long millis) and join(Duration) (Java 19) wait at most the given time. join(Duration) returns true if the thread terminated in time. The older join(long) returns nothing, so check t.isAlive() afterwards.
Why most code shouldn't call new Thread() at all
Creating platform threads by hand works for a demo, but real programs run thousands of tasks. Since Java 5, the standard answer has been an executor: you submit tasks, and the executor decides which thread runs them. A thread pool keeps a fixed set of threads alive and reuses them, so you don't pay the creation cost per task, and the pool size caps how many run at once.
try (ExecutorService pool = Executors.newFixedThreadPool(4)) {
for (int i = 1; i <= 8; i++) {
int jobId = i;
pool.submit(() -> System.out.println("job " + jobId + " on " + Thread.currentThread().getName()));
}
} // close() waits for all submitted jobs, then shuts the pool down
Eight jobs share four threads named pool-1-thread-1 to pool-1-thread-4. Which thread runs which job varies from run to run. ExecutorService has implemented AutoCloseable since Java 19, so try-with-resources waits for the submitted work and shuts the pool down. Article 5 covers executors and their limits, and Part 3 builds a thread pool from scratch.
With virtual threads, the advice changes slightly: don't pool virtual threads. They are cheap enough to create one per task, so you use Executors.newVirtualThreadPerTaskExecutor() or Thread.ofVirtual() instead of a pool. Part 2 explains why.
Handling InterruptedException correctly
Java has no safe way to force a thread to stop. (Thread.stop() was deprecated in Java 1.2 and has thrown UnsupportedOperationException since Java 20.) Instead, cancellation is cooperative: one thread calls worker.interrupt(), which sets the worker's interrupt flag. The worker notices in one of two ways:
- Blocking methods such as
Thread.sleep,Object.wait,Thread.join,BlockingQueue.takeandFuture.getthrowInterruptedException. If the flag is already set when they would block, they throw immediately. AFuture.get()on a future that has already completed just returns the result. - Code that doesn't block can check
Thread.currentThread().isInterrupted()in its loop.
The detail that trips people up is that throwing InterruptedException clears the flag. If you catch the exception and carry on, the request to stop is lost, and code further up the stack can't see it. The rule is:
- If you can, don't catch it: declare
throws InterruptedExceptionand let it propagate. - If you must catch it (for example inside a
Runnable, whoseruncan't throw checked exceptions), restore the flag withThread.currentThread().interrupt()and then stop what you're doing, usually by throwing an unchecked exception that wraps the original. - Never swallow it with an empty
catchblock.
This program interrupts a sleeping worker and shows the flag being cleared and restored:
import java.util.concurrent.CountDownLatch;
public class Solution {
// A helper that can't declare InterruptedException (e.g. it implements Runnable)
static void pause(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
System.out.println(" caught; flag after catch = " + Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt(); // restore the flag
System.out.println(" flag after restore = " + Thread.currentThread().isInterrupted());
throw new IllegalStateException("pause interrupted", e);
}
}
public static void main(String[] args) throws InterruptedException {
CountDownLatch started = new CountDownLatch(1);
Thread worker = new Thread(() -> {
started.countDown();
try {
pause(10_000);
System.out.println(" finished normally");
} catch (IllegalStateException e) {
System.out.println(" worker stops: " + e.getMessage()
+ ", still interrupted = " + Thread.currentThread().isInterrupted());
}
}, "sleepy-worker");
worker.start();
started.await();
System.out.println("main interrupts " + worker.getName());
worker.interrupt();
worker.join();
System.out.println("worker state: " + worker.getState());
}
}
Output:
main interrupts sleepy-worker
caught; flag after catch = false
flag after restore = true
worker stops: pause interrupted, still interrupted = true
worker state: TERMINATED
The output doesn't depend on timing. If the interrupt arrives before the worker reaches sleep, sleep sees the flag already set and throws at once. The ten-second sleep never completes in either case, so the program ends immediately.
What's current
Thread.Builder,Thread.ofPlatform()andThread.ofVirtual()are final since Java 21. So areThread.startVirtualThread(Runnable)andThread.isVirtual().Thread.threadId()(Java 19) replaces the deprecatedgetId(), which could be overridden.Thread.stop()still exists but always throwsUnsupportedOperationException(since Java 20), and the relatedThreadDeathclass is deprecated for removal.suspend()andresume()are gone entirely: they no longer exist in Java 25.
Common questions
Is it wrong to extend Thread?
Not wrong, just rarely the best choice. Implementing Runnable keeps the task reusable on any kind of thread or executor and leaves your class free to extend something else.
Why can't a thread be restarted?
A thread's lifecycle runs one way: NEW, then RUNNABLE (possibly blocked or waiting along the way), then TERMINATED. The JVM releases the thread's stack and OS resources when it terminates. To run the same work again, create a new thread from the same Runnable.
When is it OK to ignore InterruptedException?
Almost never. A short demo main that nobody will ever interrupt is about the only safe place. In library and application code, always restore the flag or propagate the exception, because you don't know who is trying to cancel the work.
What's the difference between isInterrupted() and Thread.interrupted()?
t.isInterrupted() reads t's flag without changing it. The static Thread.interrupted() reads the current thread's flag and clears it. Use the static version only when you are about to handle the interruption yourself.
Summary
- Describe work as a
Runnable(usually a lambda) and hand it to a thread or an executor. start()creates a new thread that callsrun(). Callingrun()directly runs the task on the current thread.- A thread can be started only once.
- Since Java 21,
Thread.ofPlatform()andThread.ofVirtual()builders configure names, daemon status and more, and can produce aThreadFactory. - Real programs use executors rather than
new Thread(). Platform threads are pooled, while virtual threads are created per task. - Cancellation is cooperative through the interrupt flag. If you catch
InterruptedException, restore the flag withThread.currentThread().interrupt().
Practice questions
What does this program print?
public class Solution {
public static void main(String[] args) {
Thread t = new Thread(() -> System.out.println(Thread.currentThread().getName()), "helper");
t.run();
System.out.println(t.getState());
}
}
Answer
main
NEW
run() executes the lambda on the current thread, main. The helper thread was never started, so it's still in the NEW state.
A task catches InterruptedException from queue.take() and just logs it, then continues its while (true) loop. What goes wrong?
Answer
The exception cleared the interrupt flag, so the cancellation request is lost. The loop calls take() again and blocks forever. A shutdown that relies on interrupting the worker, such as ExecutorService.shutdownNow(), never completes. The fix is to restore the flag and exit the loop (or make the loop condition check isInterrupted()).
Which builder call creates three platform threads named scan-0, scan-1 and scan-2?
Answer
Create one builder with Thread.ofPlatform().name("scan-", 0) and call start(task) on it three times. A builder created with a prefix and start value increments the counter each time it creates a thread.
Previous: Java Threads Explained: Concurrency vs Parallelism and Why Every Java Program Is Multithreaded
Next: The Hidden Cost of Java Platform Threads: Memory, OS Limits and Context Switches

More in Modern Java Concurrency
Quiz: Java Concurrency Changes From Java 21 to Java 28
Sixteen questions on what changed release by release: virtual threads final in 21, synchronized without pinning in 24, scoped values final in 25, and the long road of structured concurrency from subclasses to open() and Joiner, the Java 26 and 27 refinements, and final in 28.
Quiz: Java Concurrency's Delicate Details
Twenty edge cases of modern Java concurrency that surprise experienced developers: virtual thread priorities and names, semaphores without owners, null scoped values, cancelling a CompletableFuture, pool sizing with unbounded queues, and StructuredTaskScope's strict rules.
Quiz: Java Concurrency Interview Questions (Virtual Threads, Structured Concurrency, Scoped Values)
Twenty questions of the kind interviewers ask about modern Java concurrency: platform vs virtual threads, pinning, pools and limits, CompletableFuture, structured concurrency, scoped values vs ThreadLocal, reactive programming, and production diagnostics.
Quiz: Modern Java Concurrency Final Exam (All 7 Parts)
The final exam for the modern Java concurrency course: thirty questions across all seven parts, from platform threads, pools and CompletableFuture through virtual threads, the scheduler, structured concurrency, scoped values, reactive streams and frameworks.
Discussion
Sign in to ask a question or share what you learned.