Quick revision
The short answer
We can extend Thread and override run(), pass a Runnable to new Thread(runnable).start(), use the Thread.Builder API added in Java 21, such as Thread.ofPlatform().name("worker-", 0).start(task) or Thread.ofVirtual().start(task) (and the shortcut Thread.startVirtualThread(task)), or submit tasks to an ExecutorService such as Executors.newFixedThreadPool(4) or Executors.newVirtualThreadPerTaskExecutor() inside try-with-resources. In application code we usually submit tasks to executors, which manage lifecycle, naming and errors. Calling run() instead of start() is a bug because run() just executes the task on the calling thread; only start() creates a new thread, and calling start() twice throws IllegalThreadStateException.
What to remember
In this answer, we saw four ways to start a thread in Java 25: extending Thread, passing a Runnable, the Thread.Builder API for platform and virtual threads, and an ExecutorService, which is AutoCloseable since Java 19. Application code should usually submit tasks to executors, which manage lifecycle, naming and errors. Calling run() runs the task on the caller without a new thread, and calling start() twice throws IllegalThreadStateException. Virtual threads are named through Thread.ofVirtual().name(...) and an executor built from its factory.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We can extend Thread and override run(), pass a Runnable to new Thread(runnable).start(), use the Thread.Builder API added in Java 21, such as Thread.ofPlatform().name("worker-", 0).start(task) or Thread.ofVirtual().start(task) (and the shortcut Thread.startVirtualThread(task)), or submit tasks to an ExecutorService such as Executors.newFixedThreadPool(4) or Executors.newVirtualThreadPerTaskExecutor() inside try-with-resources. In application code we usually submit tasks to executors, which manage lifecycle, naming and errors. Calling run() instead of start() is a bug because run() just executes the task on the calling thread; only start() creates a new thread, and calling start() twice throws IllegalThreadStateException.
Why this question is asked
Starting a thread is one of the first things we learn in Java, and yet each way to do it says something different about who owns the thread, how it is named and what happens to its errors. Java 21 added builders and virtual threads, so the options in Java 25 are wider than most tutorials show. Interviewers ask this question to see whether we know the modern options and whether we can explain why executors are the default choice. In this answer, we will go through the four ways with code, compare them, look at the run() versus start() bug, and answer the follow-up about naming virtual threads.
1Way 1 and 2: Extending Thread or Passing a Runnable
The oldest way is to subclass Thread and override run(). The more common one is to keep the task separate as a Runnable, often a lambda, and hand it to a Thread.
Extending Thread mixes two things, the work to do and the mechanism that runs it, so the task cannot be reused with an executor and the class cannot extend anything else. It is rarely needed. Passing a Runnable keeps them separate, which is the reason the Runnable form became standard.
2Way 3: The Thread.Builder API (Java 21 and Later)
Java 21 added builders for both kinds of thread. Thread.ofPlatform() builds a platform thread, an ordinary OS-backed thread, and Thread.ofVirtual() builds a virtual thread, a lightweight thread managed by the JVM that unmounts from its carrier while blocked. Both builders let us set a name, an uncaught exception handler and more, then start or create the thread.
The name(prefix, start) form appends a counter to each thread the builder creates, which is especially useful through factory(), which turns the builder into a ThreadFactory we can give to an executor.
3Way 4: An ExecutorService
The fourth way does not create threads in our code at all. We submit tasks, and the executor decides which thread runs them. A fixed pool reuses a set number of platform threads; a virtual-thread-per-task executor creates a new virtual thread for every task. Since Java 19, ExecutorService is AutoCloseable, and close() waits for submitted tasks to finish, so try-with-resources gives a clean lifecycle.
| Way | Who owns the thread | Good for |
|---|---|---|
| Extend Thread | Our subclass | Rarely: legacy or teaching |
new Thread(runnable) | Our code | Simple one-off threads |
Thread.ofPlatform / ofVirtual | Our code, configured | Named threads, factories |
ExecutorService | The executor | Application code: lifecycle, reuse, errors |
4Which One to Use in Application Code
In application code we normally submit tasks to an executor. The executor owns the threads' lifecycle, so shutdown is one call rather than a hunt for loose threads. It can be bounded, so a traffic spike cannot create unlimited platform threads. It is configured with a named thread factory once, and it gives us Futures to collect results and errors. In Spring, we would inject a ThreadPoolTaskExecutor or use @Async rather than build executors by hand. For I/O-bound work on Java 21 and later, newVirtualThreadPerTaskExecutor() gives the simplicity of a thread per task with almost no cost. Raw threads, even built with the builder, are for the rare case where we need a single long-lived dedicated thread and manage its lifecycle ourselves.
5Why Calling run() Instead of start() Is a Bug
start() asks the JVM to create a new thread and that new thread then calls run(). Calling run() ourselves is just a normal method call: the task runs on the current thread, synchronously, and no new thread is created. The code compiles and produces the right output, so the bug often goes unnoticed until someone wonders why nothing runs in parallel.
- Did we call start()?YesJVM creates a new threadNorun() executes on the caller
- New thread calls run()caller continues at once, in parallel
- start() called again later?YesIllegalThreadStateExceptionNoThread ends normally
3 steps in order. Play them, or step through them one at a time.
Notice the last line. A Thread object represents one execution. After it has been started, starting it again throws IllegalThreadStateException, even after it finished. To run the task again, we create a new Thread, or better, submit the task to an executor again.
6Follow-up questions
How do you give virtual threads useful names for thread dumps?
Virtual threads have an empty name by default. We name them with the builder, Thread.ofVirtual().name("order-", 0), and for an executor we turn it into a factory and use Executors.newThreadPerTaskExecutor(Thread.ofVirtual().name("order-", 0).factory()). Note that the classic jstack and jcmd Thread.print output shows platform threads, so to see virtual threads in a dump we use jcmd <pid> Thread.dump_to_file, optionally with -format=json, where the names then make sense.
Should we pool virtual threads?
No. Virtual threads are cheap to create and are meant to be one per task. Pooling them brings back the limits of a pool without its reason. If we need to limit concurrency, for example to protect a database, we use a Semaphore or the resource's own pool size.
Is Thread.startVirtualThread() the same as Thread.ofVirtual().start()?
Yes, it is a shortcut that creates and starts an unnamed virtual thread. The builder is preferable when we want a name or an uncaught exception handler.
7Conclusion
In this answer, we saw four ways to start a thread in Java 25: extending Thread, passing a Runnable, the Thread.Builder API for platform and virtual threads, and an ExecutorService, which is AutoCloseable since Java 19. Application code should usually submit tasks to executors, which manage lifecycle, naming and errors. Calling run() runs the task on the caller without a new thread, and calling start() twice throws IllegalThreadStateException. Virtual threads are named through Thread.ofVirtual().name(...) and an executor built from its factory.
Open with
There are four main ways. The oldest is to extend Thread and override run, but that mixes the task with the thread, so I almost never do it.
Then, if they want more
The usual plain version is new Thread with a Runnable, often a lambda, and then start. Since Java 21 there's the Thread.Builder API: Thread.ofPlatform().name("worker-", 0).start(task) gives a named platform thread, Thread.ofVirtual().start(task) gives a virtual thread, Thread.startVirtualThread is a shortcut, and factory() turns a builder into a ThreadFactory.
The fourth way is an ExecutorService, like Executors.newFixedThreadPool(4) or Executors.newVirtualThreadPerTaskExecutor(), used in try-with-resources because ExecutorService is AutoCloseable since Java 19 and close waits for tasks.
In application code I'd use executors, or in Spring an injected task executor or @Async, because they own the lifecycle, can be bounded, are named once through a factory and give me Futures for results and errors. Raw threads are for the rare dedicated long-lived thread.
Calling run instead of start is a bug because run is just a normal method call: the task runs synchronously on the calling thread and no new thread exists, though the output looks right. Only start creates a thread, and a thread can be started once; a second start throws IllegalThreadStateException.
For naming virtual threads, I use Thread.ofVirtual().name(prefix, 0).factory() with Executors.newThreadPerTaskExecutor, and view them with jcmd Thread.dump_to_file.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 19, Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show four ways to create and start a thread in Java 25: extending Thread, passing a...": https://scrivio-interview-library.netlify.app/java/threads/358846bd74/