[concurrency-interest] Shutting down ExecutorService

Trustin Lee tlee at redhat.com
Fri Apr 17 22:38:22 EDT 2009

Closing the ServerSocket from a different thread will cause
ServerSocket.accept() to throw an exception immediately.  Confirmed on
Sun JDK 1.5/1.6, OpenJDK, JRockit 1.5/1.6, and IBM JDK 1.5/1.6.


— Trustin Lee, http://gleamynode.net/

On Sat, Apr 18, 2009 at 3:03 AM, Thomas Barnett
<barnett.thomas at gmail.com> wrote:
> 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/
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

More information about the Concurrency-interest mailing list