How Many Ways Can We Create Threads in Java? (2025 Guide)

How Many Ways Can We Create Threads in Java? (2025 Guide)

Java provides 4 main ways to create threads + additional modern concurrency abstractions (Executors, CompletableFuture, ForkJoinPool).


1. Extending the Thread Class

This is the simplest way, but not recommended for large applications because Java does not support multiple inheritance.

✅ Example

class MyThread extends Thread { @Override public void run() { System.out.println("Thread running using Thread class"); } } public class Main { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); } }

✅ When to use

✔ Quick testing
✔ Very small programs

❌ Not recommended because

  • You lose the ability to extend another class

  • Less flexible


2. Implementing Runnable Interface (Most Common)

This is widely used because we can extend another class and still implement Runnable.

✅ Example

class MyTask implements Runnable { @Override public void run() { System.out.println("Thread running using Runnable"); } } public class Main { public static void main(String[] args) { Thread t = new Thread(new MyTask()); t.start(); } }

✅ When to use

✔ Clean separation of task vs. thread
✔ Recommended for backend apps
✔ Compatible with Executors


3. Implementing Callable Interface (with Future)

Callable is like Runnable, but it returns a value and can throw exceptions.

✅ Example

import java.util.concurrent.*; class MyCallable implements Callable<String> { @Override public String call() { return "Result from Callable"; } } public class Main { public static void main(String[] args) throws Exception { ExecutorService service = Executors.newSingleThreadExecutor(); Future<String> result = service.submit(new MyCallable()); System.out.println(result.get()); service.shutdown(); } }

✅ When to use

✔ When you need a return value
✔ When exception handling matters


4. Using ExecutorService (Modern, Scalable)

This is how real backend systems create threads.

Instead of creating threads manually, we submit tasks to a thread pool.

✅ Example

ExecutorService executor = Executors.newFixedThreadPool(3); executor.submit(() -> { System.out.println("Running in a thread pool"); }); executor.shutdown();

✅ When to use

✔ Production-grade apps
✔ Web servers / microservices
✔ High performance & resource management


5. Using CompletableFuture (Asynchronous + Functional)

The most modern approach introduced in Java 8.
Supports async pipelines, chaining, non-blocking tasks.

✅ Example

CompletableFuture.runAsync(() -> { System.out.println("Async task running"); });

Or returning values:

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); System.out.println(future.join());

✅ When to use

✔ Async programming
✔ Non-blocking services
✔ Combining results from multiple threads
✔ Microservices / reactive pipelines


6. Using ForkJoinPool (Parallel computation)

Useful for recursive tasks and parallel algorithms.

✅ Example

ForkJoinPool.commonPool().submit(() -> System.out.println("ForkJoin thread running") ).join();

✅ When to use

✔ Parallel processing
✔ Complex computations (divide & conquer)


Summary Table (Best for Blog)

MethodDescriptionWhen to UseSupports Return Value
Thread classExtend Thread and override run()Simple, basic examples❌ No
RunnableImplement task logicMost common❌ No
CallableLike Runnable but returns valueNeed result or exceptions✅ Yes
ExecutorServiceThread poolProduction-level threading✅ Yes
CompletableFutureAsync pipelineNon-blocking apps✅ Yes
ForkJoinPoolParallel computationHeavy CPU tasks✅ Yes

Diagram – Thread Creation in Java (Text-Based)

+--------------------+ | Create Thread | +--------------------+ | ----------------------------------------------------- | | | | Thread Runnable Callable Executors | | | | start() new Thread() via Executor submit(Runnable/ submit() Callable) | +----------+ | Thread | | Pool | +----------+

No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts