MediumTypes of Executors utility class in java | by Ravi Jaisawal | Medium
Sitemap

Types of Executors utility class in java

--

1. Fixed Thread Pool

-> Executors.newFixedThreadPool(n)

Use case:

  • When you have a fixed number of threads
  • Good for CPU-bound tasks
ExecutorService executor = Executors.newFixedThreadPool(3);

for (int i = 0; i < 5; i++) {
int task = i;
executor.execute(() -> {
System.out.println("Task " + task + " executed by " + Thread.currentThread().getName());
});
}

executor.shutdown();

Behavior:

  • Max n threads
  • Extra tasks go to queue
  • Prevents thread explosion

2. Cached Thread Pool

  • > Executors.newCachedThreadPool()

Use cases:

  • For short-lived, asynchronous tasks
  • When number of tasks is unpredictable
ExecutorService executor = Executors.newCachedThreadPool();

Behavior:

  • Creates new threads as needed
  • Reuses idle threads
  • Threads die after 60 sec idle

Risk: Can create too many threads → OOM

🔹 3. Single Thread Executor

→ Executors.newSingleThreadExecutor()

Use case:

  • When tasks must run sequentially
  • Ensures order of execution
ExecutorService executor = Executors.newSingleThreadExecutor();

Behavior:

  • Only 1 thread
  • Tasks executed one by one
  • Safer alternative to manual thread handling

🔹 4. Scheduled Thread Pool

→ Executors.newScheduledThreadPool(n)

Get Ravi Jaisawal’s stories in your inbox

Join Medium for free to get updates from this writer.

Use case:

For delayed or periodic tasks

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);

// Run after 5 seconds
scheduler.schedule(() -> System.out.println("Delayed Task"), 5, TimeUnit.SECONDS);

// Run every 10 seconds
scheduler.scheduleAtFixedRate(() -> System.out.println("Repeating Task"), 0, 10, TimeUnit.SECONDS);

Behavior:

  • delayed execution
  • fixed-rate execution
  • fixed-delay execution

🔹 5. Work Stealing Pool (Java 8+)

→ Executors.newWorkStealingPool()

Use case:

  • For parallel processing
  • Uses ForkJoinPool
ExecutorService executor = Executors.newWorkStealingPool();

Behavior:

  • Threads “steal” tasks from others
  • Efficient for divide-and-conquer algorithms

🔹 6. ForkJoinPool (Advanced)

(Not directly from Executors, but related)

ForkJoinPool pool = new ForkJoinPool();

Example:

import java.util.concurrent.RecursiveTask;

class SumTask extends RecursiveTask<Integer> {

private int[] arr;
private int start, end;

private static final int THRESHOLD = 3; // small task size

public SumTask(int[] arr, int start, int end) {
this.arr = arr;
this.start = start;
this.end = end;
}

@Override
protected Integer compute() {

// Base condition
if (end - start <= THRESHOLD) {
int sum = 0;
for (int i = start; i < end; i++) {
sum += arr[i];
}
return sum;
}

// Divide
int mid = (start + end) / 2;

SumTask leftTask = new SumTask(arr, start, mid);
SumTask rightTask = new SumTask(arr, mid, end);

// Fork (run asynchronously)
leftTask.fork();

// Compute right synchronously
int rightResult = rightTask.compute();

// Join left result
int leftResult = leftTask.join();

return leftResult + rightResult;
}
}

Use case:

  • Recursive tasks (e.g., merge sort)
  • Parallel streams internally use this

Summary (Very Important)

Press enter or click to view image in full size

--

--

Ravi Jaisawal
Ravi Jaisawal

Written by Ravi Jaisawal

Lead Software Engineer with 12+ years of experience architecting and delivering scalable, cloud-native distributed systems and enterprise platforms.