Lesson · Multithreading Fundamentals
Creating Threads: Runnable, Thread, Callable and the factory
14 min readCreating Threads
Java separates the work from the thing that runs it, and every route into a thread is a variation on that split. Runnable is the default, start() is the only call that makes a thread, and a ThreadFactory is where naming and policy belong.
The task comes first
Running something on another thread takes two objects, not one: the work itself, and the mechanism that runs it. Java names them separately, and almost every mistake in this lesson comes from confusing the two.
The work is a Runnable (or a Callable if it must return a value). The mechanism is a Thread, or an executor that owns threads and hands them tasks. Write the task once, and it can run on a new thread, on a pool, or inline in a test.
Runnable task = () -> work();creates a task object. Nothing runs yet; it is a value holding a method to call.new Thread(task, "worker-1")creates a Thread object in state NEW. Still nothing runs.start()asks for an operating system thread. The new thread callsrun()while the caller continues immediately.- When
run()returns, the thread terminates and its stack is gone. A Thread object runs once.
Three routes to a running thread
You will see all three in real code, and they differ in what they cost you. Extending Thread welds your class to the mechanism. Implementing Runnable keeps the task separate and reusable. Submitting a Callable to an executor hands the thread management to the executor and gets a result back.
The choice is not about which one compiles. It is about whether the task can be reused somewhere else, and whether you need a return value.
| Route | You write | You get | The catch |
|---|---|---|---|
| extends Thread | class Worker extends Thread { run() } | a thread that is also the task | the class cannot extend anything else, and there is no task object to reuse |
| implements Runnable | Runnable task = () -> ... | a reusable task, run() returns void | you still have to hand it to a thread or a pool |
| Callable + executor | Callable<Integer> task = () -> ... | a value, plus checked exceptions | needs submit(), and the result arrives through Future.get() |
Route map · from a task to a running thread
each box is one route into the same destinationEvery route ends in the same place: a thread that executes the task and then terminates. What differs is what the route gives back, and how tightly your class is bound to the mechanism.
class Worker extends Thread { run() }
- run(): void
- task and mechanism are the same object
cannot extend anything else, and nothing is left to reuse
new Worker().start()
Runnable task = () -> work();
- run(): void
- a task object, reusable on a Thread, a pool or inline
new Thread(task, "worker-1").start()
Callable<Integer> task = () -> sum();
- call(): V, and may throw checked exceptions
- the executor owns the threads
pool.submit(task)
Future<V> comes back for get()
named by the factory, scheduled by the OS, RUNNABLE in a dump, and single use
Thread and Runnable both end at start(). The Callable route ends at the thread the executor opens for it, and the value travels back through a Future.
Why Runnable is the default
Runnable is the smallest possible description of a task: no return value, no checked exceptions, no tie to the mechanism. That smallness is the point, and it buys four practical things.
- Your class stays free: a class implementing Runnable can still extend whatever it needs, which extending Thread forbids.
- The task becomes reusable: the same Runnable can go to a
new Thread, to an executor, or be called inline astask.run()in a test. - Lambdas and method references land here:
Runnable r = () -> send(email);reads as the work, with no wrapper class in the way. - Executors take Runnable directly:
pool.execute(task)is the same object, and the pool decides which thread runs it.
Separate the task from the mechanism so the same body can run anywhere. Once the work is a Runnable, choosing a thread or a pool is somebody else's decision.
Name the thread, every time
new Thread(task, "worker-1") costs nothing and repays the first time you read a stack trace. An unnamed thread is Thread-3, and a dump or a log line full of Thread-3 and pool-1-thread-7 tells you nothing about which work it was doing.
When threads are created in more than one place, the naming rule does not belong at every call site. A ThreadFactory is the policy object: it receives the task and returns a configured Thread, which is where naming, the daemon flag, priority and the uncaught-exception handler go once.
This lesson stops at the factory. What the daemon flag actually changes, and what happens when a task throws, are their own topics later in this part.
- Name:
"worker-1","http-reader-3", or a factory that numbers them. A name is the cheapest debugging tool in the JVM. - Policy lives in the factory: daemon or not, priority, the uncaught-exception handler (all covered in the lessons that follow).
- One factory per role:
http-reader-Nanddb-writer-Nin a dump tell the whole story at a glance.
run() is a method, start() is a thread
start() does three things: it moves the thread from NEW to RUNNABLE, it asks the operating system for a real thread, and that thread calls run(). The caller does not wait: the next line of main executes while the new thread works.
run() does none of that. It is an ordinary method with an unfortunate name, so new Thread(task).run() executes the body on whatever thread called it, usually main. The program works, the output looks plausible, and nothing is concurrent.
A Thread object runs once. Calling start() a second time throws IllegalThreadStateException, which is the JVM telling you that a thread is not a reusable worker: for reuse, hand tasks to an executor instead.
Creating threads · three routes, one runtime
pick a route, then try run versus startthe task as an object, the thread as the mechanism
you write
Runnable task = () -> System.out.println(
"running on " + Thread.currentThread().getName());
new Thread(task, "worker-1").start();console
worker-1running on worker-1
The same Runnable can go to a new Thread, to an executor or to a test that calls run() inline, and the console shows the name you chose.
same body · does a thread appear?
worker-1task body starts
mainstart() returned, main continues
worker-1task body ends
start() asked the OS for a thread and returned immediately. Two threads now make progress independently; the last two lines can swap places, because the scheduler decides.
Callable and Future: work that returns
Callable<V> is Runnable with a return value and checked exceptions: V call() throws Exception. You cannot start one with a plain Thread, because a Thread's run() returns void. A Callable is always submitted to an executor, and submit() returns a Future<V> handle immediately.
The Future is a promise, not the answer. get() blocks until the task finishes and returns the value, or throws ExecutionException wrapping whatever the task threw. get(timeout, unit) gives up after a deadline instead of waiting forever.
| Runnable | Callable<V> | |
|---|---|---|
| Method | void run() | V call() throws Exception |
| Returns | nothing | a value of type V |
| Checked exceptions | cannot throw them | may throw; delivered through the Future |
| How it runs | new Thread(task), pool.execute(task), task.run() | pool.submit(task) only |
| Result handle | none | Future<V>: get() blocks, get(timeout) gives up |
One-line threads and the executor path
Java 21's builders remove the constructor noise. Thread.ofPlatform().name("worker-1").start(task) creates and starts a named platform thread in one expression, and .daemon(true) or .priority(...) chain the same way the factory would.
The same builder offers Thread.ofVirtual(), which starts a virtual thread instead: scheduled by the JVM and multiplexed onto a small pool of carrier threads, so blocking is cheap. That is a later part of the course, and the one-line shape above is deliberately the same.
For more than a handful of tasks, stop creating threads by hand. An executor owns its threads, takes tasks through execute() or submit(), and gives you shutdown and result handling. Executors.newFixedThreadPool(4) creates one; the executor lessons cover sizing it.
Thread creation is easy to write and expensive to get wrong. Name every thread, keep the task a Runnable, and let an executor own the threads as soon as there is more than one task per request.
What to remember
Four rules carry this whole topic, and they are worth saying out loud before the interview angles below.
- Prefer Runnable: the task stays separable, reusable and free to extend another class.
- `start()`, never `run()`: run() is a method call on the caller; start() is the only call that creates a thread, and a Thread starts once.
- Name the thread, or own a factory: a dump is only readable if the names are yours.
- Need a value?
Callableplussubmit()gives a Future; Runnable is for fire-and-forget work.
start() is the method that makes a thread; run() is just a method. Everything else in this lesson follows from that sentence.
run() does not start a thread
`new Thread(task).run()` compiles, runs, prints and passes the test, and it never creates a thread: the body executed on the caller. Nothing warns you, the output looks right, and the bug appears later as a UI freeze or a slow loop. start() is the only call that asks for a thread.
Interview angles
Runnable versus extending Thread: why prefer Runnable?+
Runnable keeps the task separate from the mechanism: the class can still extend something else, the same task object can run on a new thread, on an executor or inline, and lambdas make it shorter than the subclass.
What does start() do that run() does not?+
start() moves the thread from NEW to RUNNABLE, asks the operating system for a real thread, and lets that thread call run() asynchronously. run() is an ordinary method: it executes on the caller and returns normally.
Common misconception: Many candidates describe run() as 'starting the thread'. It never does. If a code review shows new Thread(task).run(), you are reading a method call, not concurrency.
What happens if you call start() twice?+
The second call throws IllegalThreadStateException. A Thread is a single-use object: once it has run, it is TERMINATED for good, and reuse means a new Thread object or, better, an executor that pools them.
When do you need Callable instead of Runnable?+
When the task returns a value or throws a checked exception. Callable goes to an executor with submit(), which returns a Future; get() blocks for the result and wraps task failures in ExecutionException.
It bounds the wait: instead of blocking forever it throws TimeoutException, which is what you want in a request path. The task itself keeps running unless you cancel it.
Follow-up: What does get(timeout, unit) change?
Why a ThreadFactory instead of a constructor call at each site?+
The factory is the one place the thread policy lives: naming, the daemon flag, priority, the uncaught-exception handler. Every thread created through it is consistent, and a dump or log becomes readable because the names are meaningful.
How do you run work on a pool instead of a raw thread?+
Hand the task to the executor: pool.execute(runnable) for fire-and-forget, pool.submit(runnableOrCallable) when you want a Future. The executor owns and reuses its threads, so per-task thread creation disappears.