Executors and Thread Pools
An Introduction with Examples in Java |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
sendMessage()
method in
the Dispatcher
is being executed in the
caller's thread of executionRealTimeDispatchHandler
and
the DailyDispatchHandler
sendMessage()
add tasks to a queue
and return "immediately"Executor
ObjectsThread
is being used, we can't`
call its interrupt()
or join()
methods)Executor
interface (i.e., create a subinterface)
ScheduledExecutorService
Runnable
is void
, the
tasks can't return anything (except indirectly through reference
parameters that are passed in)get()
- wait for the task to complete
(perhaps for a maximum amount of time)isDone()
- determine if the task is completecancel()
- attempt to cancel the taskisCancelled()
- determine if the task was cancelled
before completionFuture
can be used like a
latch
Future
Future
object can be useful even when
the task does not return anything since it provides other
functionalityFuture
object has a cancel()
methodFuture
object can be parameterized
using ?
to indicate that we don't
care about the return type
Terminator
Task
Terminator
TaskExecutor
and ExecutorService
:
Runnable
to an Executor
happen-before its execution beginsCallable
to an ExecutorService
happen-before its execution beginsFuture
:
Future
happen-before actions subsequent
to the retrieval of the result via Future.get()
in another thread