How do I create a thread pool with Executors in Java?

In Java, the java.util.concurrent.Executors class provides factory methods for creating and managing thread pools easily. Below are common ways to create a thread pool using Executors:

1. Fixed Thread Pool

A fixed thread pool contains a fixed number of threads. This is useful when you have a specific number of tasks to manage and want to limit the number of concurrently running threads.

package org.kodejava.util.concurrent;

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

public class FixedThreadPoolExample {
    public static void main(String[] args) {
        // Create a fixed thread pool with 3 threads
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            fixedThreadPool.execute(() -> {
                System.out.println("Task " + taskId + " is running in thread " + Thread.currentThread().getName());
            });
        }

        // Shutdown the pool after task submission
        fixedThreadPool.shutdown();
    }
}

2. Cached Thread Pool

A cached thread pool creates new threads as needed and reuses previously constructed threads (if available). This is suitable for executing many short-lived asynchronous tasks.

package org.kodejava.util.concurrent;

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

public class CachedThreadPoolExample {
    public static void main(String[] args) {
        // Create a cached thread pool
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            cachedThreadPool.execute(() -> {
                System.out.println("Task " + taskId + " is running in thread " + Thread.currentThread().getName());
            });
        }

        // Shutdown the pool after task submission
        cachedThreadPool.shutdown();
    }
}

3. Single Thread Executor

A single-threaded executor ensures that tasks are executed sequentially, one at a time, in a single thread.

package org.kodejava.util.concurrent;

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

public class SingleThreadExecutorExample {
    public static void main(String[] args) {
        // Create a single-threaded executor
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

        for (int i = 1; i <= 5; i++) {
            final int taskId = i;
            singleThreadExecutor.execute(() -> {
                System.out.println("Task " + taskId + " is running in thread " + Thread.currentThread().getName());
            });
        }

        // Shutdown the pool after task submission
        singleThreadExecutor.shutdown();
    }
}

4. Scheduled Thread Pool

A scheduled thread pool is used to schedule tasks to run after a delay or periodically.

package org.kodejava.util.concurrent;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolExample {
    public static void main(String[] args) {
        // Create a scheduled thread pool with 2 threads
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);

        // Schedule a task to run after a 3-second delay
        scheduledThreadPool.schedule(() -> {
            System.out.println("Task is running after a delay in thread " + Thread.currentThread().getName());
        }, 3, TimeUnit.SECONDS);

        // Schedule a repeating task to run every 2 seconds
        scheduledThreadPool.scheduleAtFixedRate(() -> {
            System.out.println("Repeating task is running in thread " + Thread.currentThread().getName());
        }, 1, 2, TimeUnit.SECONDS);

        // Optionally, shutdown the pool after some time (e.g., 10 seconds)
        scheduledThreadPool.schedule(() -> scheduledThreadPool.shutdown(), 10, TimeUnit.SECONDS);
    }
}

5. Custom Thread Pool

For more advanced needs, you can use ThreadPoolExecutor directly to fine-tune the behavior of the thread pool.

package org.kodejava.util.concurrent;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CustomThreadPoolExample {
    public static void main(String[] args) {
        // Create a custom thread pool with 2 core threads, 4 maximum threads, and a 10-task queue
        ThreadPoolExecutor customThreadPool = new ThreadPoolExecutor(
                2, 4, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10));

        for (int i = 1; i <= 10; i++) {
            final int taskId = i;
            customThreadPool.execute(() -> {
                System.out.println("Task " + taskId + " is running in thread " + Thread.currentThread().getName());
            });
        }

        // Shutdown the pool after task submission
        customThreadPool.shutdown();
    }
}

Key Points:

  • shutdown(): Prevents new tasks from being submitted to the thread pool and initiates an orderly shutdown.
  • shutdownNow(): Attempts to stop all actively executing tasks and halts the processing of waiting tasks.
  • newFixedThreadPool(): Creates a pool of a fixed number of threads.
  • newCachedThreadPool(): Creates a pool with potentially unlimited threads.
  • newSingleThreadExecutor(): Creates a single-threaded pool.
  • newScheduledThreadPool(): Creates a pool for scheduling tasks.

By using thread pools, you can effectively manage system resources and control the level of concurrency in your applications.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.