How do I submit multiple tasks and get results using invokeAll?

To submit multiple tasks and get results using invokeAll in Java, you can make use of the ExecutorService. The invokeAll method submits a collection of Callable tasks to the executor and waits for all of them to complete. Once completed, it returns a list of Future objects, each representing the result of a corresponding task.

Here’s how it works:

  1. Create a collection of Callable tasks: These tasks are units of work that the executor will execute in parallel.
  2. Submit the tasks using invokeAll: The invokeAll method blocks until all tasks are complete or timed out.
  3. Retrieve the results from the Future objects: Each Future object allows you to get the result of its corresponding task or check for exceptions.

Example Code

package org.kodejava.util.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

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

      // Create a collection of Callable tasks
      List<Callable<String>> tasks = new ArrayList<>();
      tasks.add(() -> {
         // Simulate doing some work
         Thread.sleep(1000);
         return "Task 1 completed";
      });
      tasks.add(() -> {
         Thread.sleep(2000);
         return "Task 2 completed";
      });
      tasks.add(() -> {
         Thread.sleep(1500);
         return "Task 3 completed";
      });

      try {
         // Submit the tasks and wait for all of them to complete
         List<Future<String>> results = executorService.invokeAll(tasks);

         // Iterate through the futures to retrieve the results
         for (Future<String> future : results) {
            try {
               // Get the result of each task
               System.out.println(future.get());
            } catch (ExecutionException e) {
               System.err.println("Task encountered an issue: " + e.getMessage());
            }
         }
      } catch (InterruptedException e) {
         System.err.println("Task execution was interrupted: " + e.getMessage());
      } finally {
         // Shutdown the executor service
         executorService.shutdown();
      }
   }
}

Explanation:

  1. ExecutorService:
    • A thread pool is created (Executors.newFixedThreadPool(3)), which allows up to 3 threads to run simultaneously.
  2. List of Callable tasks:
    • Each task implements the Callable interface and returns a result. For example, the tasks simulate work by Thread.sleep() and return a string.
  3. invokeAll Method:
    • executorService.invokeAll(tasks) submits all tasks at once and blocks until all tasks are complete.
  4. Retrieving Results:
    • The method returns a list of Future objects, where future.get() is used to retrieve the result of each task.
  5. Exceptions:
    • Handle InterruptedException (if the current thread is interrupted) and ExecutionException (if a task fails with an exception).
  6. Shutdown the Executor:
    • Always call shutdown() to properly terminate the executor service and release resources.

Output:

Task 1 completed
Task 3 completed
Task 2 completed

(Note: The order may vary since the tasks run concurrently.)

Keynotes:

  • Use ExecutorService to manage thread pools efficiently.
  • The invokeAll method blocks until all tasks are complete.
  • Handle exceptions like InterruptedException and ExecutionException.
  • Always shut down the executor service to free resources.

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.