Anda di halaman 1dari 3

Executors

An executor is an object that executes submitted Runnable tasks.

Executors are mainly used to separate thread management and creation from the rest of
the application.

1. Executor Interfaces
The java.util.concurrent package defines three executor interfaces:
Executor - a simple interface that supports launching new tasks
ExecutorService - a subinterface of Executor, which adds features that help
manage the lifecycle, both of the individual tasks and of the executor itself
ScheduledExecutorService - a subinterface of ExecutorService, supports future
and/or periodic execution of tasks
It is important to note that variables that refer to executor objects are generally
declared as one of these three interface types, and not with an executor class type.

1.1 The Executor Interface


The Executor interface provides a single method (execute()) designed to be a drop-in
replacement for a common thread-creation idiom:
voidexecute(Runnablecommand)
Depending on the Executor implementation, the execute() method may:
create a thread and launch it immediately
use an existing worker to run the the command
place the command in a queue to wait for a worker thread to become available

1.2 The ExecutorService Interface


The ExecutorService interface supplements execute() with a similar, but more
versatile submit() method:
<T>Future<T>submit(Callable<T>task)
Future<?>submit(Runnabletask)
<T>Future<T>submit(Runnabletask,Tresult)
The submit() method accept both Runnable and Callabe objects. Callable objects
allow the task to return a value.

The submit() method returns a Future object, which is used to retrieve the Callable
return value and to manage the status of both Callable and Runnable tasks.

The ExecutorService interface also provides methods for:


submitting large collections of Callable objects (the invokeAll() and
invokeAny() methods)
managing the shutdown of the executor (the awaitTermination(), isShutdown(),
isTerminated(), shutdown(), and shutdownNow() methods)
It is important to note that to support immediate shutdown, tasks should handle
interrupts correctly.

1.3 The ScheduledExecutorService Interface


The ScheduledExecutorService interface supplements the methods of its parent
interface with the schedule() method:
<V>ScheduledFuture<V>schedule(Callable<V>callable,longdelay,
TimeUnitunit)
ScheduledFuture<?>schedule(Runnablecommand,longdelay,
TimeUnitunit)
The schedule() method executes a Runnable or Callable task after a specified delay.

In addition, the interface defines scheduleAtFixedRate() and


scheduleWithFixedDelay(), which are used to execute the specified tasks repeatedly,
at defined intervals.

2. Thread Pools
Most of the executor implementations in java.util.concurrent use thread pools,
which consist of worker threads.

A thread pool exists separately from the Runnable and Callable tasks it executes and is
often used to execute multiple tasks.

Using worker thread minimizes the overhead due to thread creation. Thread objects use
a significant amount of memory.

Types of thread pools:


fixed thread pools
cached thread pools
single thread executors

One common type of thread is the fixed thread pool. This type of pool always has a
specified number of threads running. If a thread is somehow terminated while it is still in
use, it is automatically replaced with a new thread. Tasks are submitted to the pool via
an internal queue, which holds extra tasks whenever there are more active tasks than
threads.

The java.util.concurrent.Executors class provides the following factory methods:


newFixedThreadPool() - creates an executor with a fixed thread pool
newCachedThreadPool() - creates an executor with an expandable thread pool
newSingleThreadExecutor() - creates an executor that executres a single task at
a time
other factory methods return ScheduledExecutorService versions of the above
executors
If none of the above factory methods meet the needs of the user, constructing instances
of ThreadPoolExecutor or ScheduledThreadPoolExecutor will provide additional
options.

3. Fork/Join
The fork/join framework is an implementation of the ExecutorService interface that can
be used to take advantage of multiple processors. It is designed for work that can be
broken into smaller pieces recursively.

As with any ExecutorService implementation, the fork/join framework distributes tasks


to worker threads in a thread pool. The fork/join framework is distinct because it uses a
"work-stealing" algorithm - worker threads that run out of things to do can steal tasks
from other threads that are still busy.

The center of the fork/join framework is the ForkJoinPool class, an extension of the
AbstractExecutorService class. ForkJoinPool implements the core work-stealing
algorithm and can execute ForkJoinTask processes.

Anda mungkin juga menyukai