Java Reference
In-Depth Information
shut down until all queued tasks are processed. Other thread-owning services should consider
providing a similar choice of shutdown modes.
Simple programs can get away with starting and shutting down a global
ExecutorSer-
vice
from
main
. More sophisticated programs are likely to encapsulate an
Execut-
orService
behind a higher-level service that provides its own lifecycle methods, such as
stead of managing its own threads. Encapsulating an
ExecutorService
extends the own-
ership chain from application to service to thread by adding another link; each member of the
chain manages the lifecycle of the services or threads it owns.
Listing 7.16. Logging Service that Uses an
ExecutorService
.
7.2.3. Poison Pills
Another way to convince a producer-consumer service to shut down is with a
poison pill
:
a recognizable object placed on the queue that means “when you get this, stop.” With a
FIFO queue, poison pills ensure that consumers finish the work on their queue before shut-