The FixedThreadPoolExecutor is a type of ThreadPoolExecutor in the Executor framework that executes each submitted task using one of a fixed number of threads. It maintains a pool of threads and reuses them to execute submitted tasks as they become available. Here are some of the supported methods in FixedThreadPoolExecutor:

  1. execute(Runnable command): Executes the given task sometime in the future. The task is run in a thread from the thread pool.
  2. submit(Callable<T> task): Submits a task for execution and returns a Future representing the pending result of the task.
  3. shutdown(): Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
  4. awaitTermination(long timeout, TimeUnit unit): Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
  5. getActiveCount(): Returns the approximate number of threads that are actively executing tasks.
  6. getTaskCount(): Returns the approximate total number of tasks that have been scheduled for execution.
  7. getCompletedTaskCount(): Returns the approximate total number of tasks that have completed execution.
  8. getQueue(): Returns the queue used by this executor. This queue normally holds tasks that are waiting to be executed.

Here’s an example of using FixedThreadPoolExecutor:

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 5 threads
        ExecutorService executor = Executors.newFixedThreadPool(5);

        // submit 10 tasks to the pool
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("" + i);
            executor.execute(worker);
        }

        // shutdown the pool
        executor.shutdown();
        while (!executor.isTerminated()) {
        }

        System.out.println("Finished all threads");
    }
}

class WorkerThread implements Runnable {

    private String message;

    public WorkerThread(String message) {
        this.message = message;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName() + " (Start) message = " + message);
        processMessage();
        System.out.println(Thread.currentThread().getName() + " (End)");
    }

    private void processMessage() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

In this example, a FixedThreadPoolExecutor with 5 threads is created using the newFixedThreadPool() method of the Executors class. Then, 10 tasks (represented by WorkerThread objects) are submitted to the pool using the execute() method of the ExecutorService interface.

Each WorkerThread object prints a message to the console, sleeps for 2 seconds, and then prints another message to indicate that it has finished its work.

After all tasks are submitted, the pool is shutdown using the shutdown() method of the ExecutorService interface. The program waits for all tasks to complete by calling the isTerminated() method repeatedly in a loop. Finally, a message is printed to the console to indicate that all threads have finished their work.