← Back to Java

Creating Threads in Java

JavaEntryjava

The Question

What are the different ways to create a thread in Java?

What a Strong Answer Covers

  • extend Thread class
  • implement Runnable interface
  • lambda as Runnable
  • Callable with Future for return value
  • ExecutorService for thread pools
  • Runnable preferred over extending Thread

Senior-Level Answer

Java provides several mechanisms to create and run threads, each with different tradeoffs.

**Extending Thread**: Subclass `java.lang.Thread` and override `run()`. Call `start()` (not `run()` directly — `run()` executes on the current thread; `start()` creates a new OS thread). Drawback: Java's single-inheritance constraint means the class cannot extend anything else.

**Implementing Runnable**: Implement `java.lang.Runnable` (functional interface with one method: `run()`). Pass an instance to `new Thread(runnable).start()`. This separates the task from the thread — you can pass the same Runnable to multiple threads or an executor. This is the preferred approach over extending Thread.

**Implementing Callable**: Like Runnable but `call()` returns a value and can throw a checked exception. Used with `ExecutorService.submit(callable)` which returns a `Future<V>`. Call `future.get()` to block until the result is available.

**ExecutorService**: The modern way. Rather than managing threads manually, submit tasks to a thread pool created by `Executors.newFixedThreadPool(n)`, `Executors.newCachedThreadPool()`, or `Executors.newScheduledThreadPool(n)`. The framework handles thread creation, reuse, and lifecycle. Always call `shutdown()` when done.

**CompletableFuture** (Java 8+): For async, non-blocking composition of tasks. `CompletableFuture.supplyAsync(supplier)` runs a task on the common ForkJoinPool. Supports chaining with `thenApply()`, `thenCompose()`, `exceptionally()`.

**Virtual Threads** (Java 21, Project Loom): `Thread.ofVirtual().start(runnable)` or `Executors.newVirtualThreadPerTaskExecutor()`. Lightweight threads scheduled by the JVM, not the OS. Enable millions of concurrent tasks without the memory cost of OS threads.

For most modern code, `ExecutorService` with `Callable`/`Runnable` is the standard. Avoid extending Thread or creating bare threads in production code.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Covers at least three approaches correctly and explains why start() must be used instead of run().

3/3 — Strong Answer

Also covers ExecutorService as the production standard, the difference between Runnable and Callable, Future for result retrieval, and optionally Virtual Threads.

Common Mistakes

  • Calling run() instead of start() — this executes the task on the current thread synchronously, not in a new thread.
  • Extending Thread unnecessarily when Runnable/Callable would suffice and preserve inheritance flexibility.
  • Creating bare threads in production code instead of using ExecutorService, which prevents proper resource management.

Follow-Up Questions

  • What is the difference between Runnable and Callable? — Runnable.run() returns void and cannot throw checked exceptions. Callable.call() returns a value and can throw checked exceptions.
  • Why should you always call executor.shutdown() or shutdownNow() when done? — ExecutorService threads are non-daemon by default; if not shut down, they keep the JVM alive after the main thread finishes.
  • What are Virtual Threads in Java 21 and when would you use them over platform threads? — Lightweight JVM-managed threads for high-concurrency I/O-bound tasks; can create millions without stack memory pressure.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout