The Executors Class – Concurrency: Part II

The Executors Class

The java.util.concurrent.Executors class provides factory and utility methods for the interfaces defined in the java.util.concurrent package. In particular, it provides factory methods that return versatile implementations of the ExecutorService (p. 1427) and the ScheduledExecutorService (p. 1440) interfaces shown in Figure 23.1. The executor services created by the methods of the Executors class are summarized in Table 23.2, showing the respective static methods, the task execution policy of the executor services they create, and examples where they are used. For many applications, these executor services will be adequate, and examples in this chapter make heavy use of them.

In addition, the java.util.concurrent.ThreadPoolExecutor class, shown in Figure 23.1, provides constructors to create even more versatile executors with various properties such as minimum and maximum pool size, keep-alive time for idle threads, a blocking queue for managing tasks, and a handler for tasks that cannot be executed.

Table 23.2 Selected Executor Services Provided by the Executors Utility Class

The static method that creates an executor that implements the ExecutorService interface (p. 1427), and the executor’s task execution policyDescription of the ExecutorService implementation
Single Thread Executor newSingleThreadExecutor() Task execution policy: tasks executed sequentially by a single thread. (Example 23.5, p. 1446)Provides a single worker thread to which tasks can be assigned. If this single thread terminates due to a failure, a new thread will take its place if needed. Tasks in the queue are guaranteed to execute sequentially. Logically it is an equivalent of a fixed thread pool of size 1, but it cannot be reconfigured to use additional threads.
Fixed Thread Pool newFixedThreadPool(int nThreads) Task execution policy: tasks executed concurrently by a fixed number of threads. (Example 23.1, p. 1429) (Example 23.2, p. 1438)Provides a thread pool of fixed size. The number of threads actively processing tasks is, at the most, equal to the pool size. The number of tasks submitted can be greater than the pool size, but some tasks may have to wait in the queue for threads to become available within the pool. If a thread terminates due to a failure, a new thread will take its place if needed. Pool threads are reused as required to execute the submitted tasks. They remain blocked when not executing a task.
Work Stealing Pool Click here to view code image newWorkStealingPool()
newWorkStealingPool(int parallelism)

Task execution policy: tasks executed concurrently while attempting to maintain its target parallelism level.
Provides a thread pool that tries to maintain its target parallelism level—that is, the maximum number of threads executing, or those available to execute tasks. The actual number of threads can vary dynamically. It makes no guarantees about the order in which the tasks will be executed. The first method uses the number of available processors as its target parallelism level.
Cached Thread Pool newCachedThreadPool() Task execution policy: tasks executed concurrently by a thread pool whose size can vary dynamically with the number of tasks remaining to execute.Provides a thread pool whose size may vary dynamically. Submitted tasks reuse available threads in the pool. Additional threads are created as needed when no threads are available. Unused threads eventually time out and are removed from the cache, so a pool that remains idle for long enough will not consume any resources.
Single Thread Scheduled Executor Click here to view code image newSingleThreadScheduledExecutor()

Task execution policy: tasks executed sequentially by a single thread according to the schedule policy specified for each task. (Example 23.5, p. 1446)
Provides a single worker thread that works very much like a combination of a scheduled thread pool of size 1 and a single thread executor. It can execute submitted tasks sequentially, using a single thread, but with a specified delay; or periodically, based on a schedule. The schedule is specified individually for each task when it is submitted to the executor.
Scheduled Thread Pool newScheduledThreadPool(
  int corePoolSize)

Task execution policy: tasks executed concurrently by a fixed number of threads, according to the schedule policy specified for each task. (Example 23.3, p. 1441) (Example 23.5, p. 1446)
Provides a thread pool of a fixed size, that can execute submitted tasks with a specified delay or periodically, based on a schedule. The schedule is specified individually for each task when it is submitted to the executor.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*

BACK TO TOP