Advanced Java October 06 ,2025

ExecutorService in Java

Introduction

In Java, ExecutorService is part of the java.util.concurrent package and provides a high-level API for managing threads.
It helps in managing a pool of threads and executing tasks asynchronously without creating individual Thread objects.

Instead of manually managing threads, ExecutorService provides a thread pool and manages scheduling, execution, and lifecycle of threads efficiently.

Key Points:

  • ExecutorService is an interface for asynchronous task execution.
  • It provides thread pooling to reuse threads and improve performance.
  • Supports scheduling, batch execution, and graceful shutdown.

How ExecutorService Works

When a task is submitted to an ExecutorService:

  • The executor finds an available thread from its thread pool.
  • If no thread is available, the task is queued.
  • Threads in the pool execute tasks and then return to the pool.

Syntax:

ExecutorService executor = Executors.newFixedThreadPool(int nThreads);

Here

  • nThreads = number of threads in the pool.

Example: Using ExecutorService

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorServiceExample {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(3); // pool of 3 threads

        Runnable task = () -> {
            System.out.println(Thread.currentThread().getName() + " is executing a task.");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " finished task.");
        };

        for (int i = 0; i < 6; i++) {
            executor.submit(task);
        }

        executor.shutdown();
    }
}

Expected Output:

pool-1-thread-1 is executing a task.
pool-1-thread-2 is executing a task.
pool-1-thread-3 is executing a task.
pool-1-thread-1 finished task.
pool-1-thread-2 finished task.
pool-1-thread-3 finished task.
pool-1-thread-1 is executing a task.
pool-1-thread-2 is executing a task.
...

Key Methods in ExecutorService

MethodDescription
submit()Submits a task for execution and returns a Future
execute()Executes a task without returning a result
shutdown()Initiates orderly shutdown
shutdownNow()Attempts to stop all actively executing tasks
invokeAll()Executes a collection of tasks
invokeAny()Executes tasks and returns the result of one

 

Advantages of CustomThreadPool

  • Improves performance by reusing threads.
  • Reduces overhead of thread creation.
  • Useful for managing high-load applications.

 Disadvantages

  • More complex to implement than using built-in ExecutorService.
  • Requires careful handling of synchronization.
  • Needs manual shutdown and cleanup.

Real-World Use Case

CustomThreadPool can be useful for:

  • Server applications handling multiple simultaneous requests.
  • Parallel batch processing tasks.
  • Any system requiring efficient thread reuse.

Conclusion

ExecutorService provides a robust way to manage threads without manually handling thread creation and lifecycle.
The custom thread pool implementation helps in understanding the core mechanics behind ExecutorService, thread pooling, and task scheduling.

For production, always use Java’s built-in Executors for stability, safety, and performance.
But building your own thread pool enhances understanding of concurrency deeply.

 

Next Blog- Custom implementation of ExecutorService in Java

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Multi...
Advanced Java August 08 ,2025

Java Multithreading...

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Semaphore...
Advanced Java October 10 ,2025

Semaphore in Java

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Producer-C...
Advanced Java October 10 ,2025

Producer-Consumer Pr...

Implementi...
Advanced Java October 10 ,2025

Implementing a Custo...

Busy Spin
Advanced Java October 10 ,2025

Busy Spin

Serializat...
Advanced Java October 10 ,2025

Serialization and De...

Segment Lo...
Advanced Java October 10 ,2025

Segment Locking in J...

Tree Bins...
Advanced Java October 10 ,2025

Tree Bins in Java

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com