Java Reference
In-Depth Information
Note
The
java.util.concurrent.ScheduledExecutorService
inter-
face extends
ExecutorService
and describes an executor that lets you schedule
tasks to run once or to execute periodically after a given delay.
Although you could create your own
Executor
,
ExecutorService
, and
ScheduledExecutorService
implementations (such as
class DirectEx-
ecutor implements Executor { public void execute(Runnable
r) { r.run(); } }
—runexecutordirectlyonthecallingthread),theconcurrency
utilities offer a simpler alternative:
java.util.concurrent.Executors
.
Tip
If you intend to create your own
ExecutorService
implementations,
you
will
find
it
helpful
to
work
with
the
and
java.util.concurrent.AbstractExecutorService
java.util.concurrent.FutureTask
classes.
The
Executors
utility class declares several class methods that return instances
ofvarious
ExecutorService
and
ScheduledExecutorService
implementa-
tions(andotherkindsofinstances).Thisclass's
static
methodsaccomplishthefol-
lowing tasks:
• Create and return an
ExecutorService
instance that is configured with
commonly used configuration settings.
• Create and return a
ScheduledExecutorService
instance that is con-
figured with commonly used configuration settings.
• Create and return a “wrapped”
ExecutorService
or
ScheduledEx-
ecutorService
instance that disables reconfiguration of the executor ser-
vice by making implementation-specific methods inaccessible.
• Createandreturna
java.util.concurrent.ThreadFactory
instance
for creating new threads.
• Create and return a
Callable
instance out of other closure-like forms so
thatitcanbeusedinexecutionmethodsrequiring
Callable
arguments(e.g.,
ExecutorService
's
submit(Callable)
method). (Check out Wikipe-
dia's “Closure (computer science)” entry [
http://en.wikipedia.org/
wiki/Closure_(computer_science)
]
to learn about closures.)
For example,
static ExecutorService newFixedThreadPool(int
nThreads)
createsathreadpoolthatreusesafixednumberofthreadsoperatingoffa
sharedunboundedqueue.Atmost,
nThreads
threadsareactivelyprocessingtasks.If