[concurrency-interest] Shutting down ExecutorService

Thomas Barnett barnett.thomas at gmail.com
Fri Apr 17 14:03:26 EDT 2009


I am trying to wrap my head around concurrency package. There is an
example of use of ExecutorService in LifecycleWebServer listing in the
book JCIP by Goetz et al [1]. It goes:

...
while(!exec.isShutDown()) {
  try {
    final Socket conn = socket.accept();
...

and has a stop() method that shuts down executor service. The problem
is, unless there is a client connection, socket.accept blocks and the
condition is never checked so LifecycleWebServer never terminates even
after calling stop().  Trying to use interruption for cancellation as
in listing 7.5 of the same book does not work either when the blocking
method is not interruptible. I just want to make sure that I am not
misunderstanding something here.

A workaround I could think of was to set timeout on server socket and
consume the SocketTimeoutException which seem to work but would that
be a correct way to do this?

Regards
Thomas

[1] http://jcip.net/


More information about the Concurrency-interest mailing list