[concurrency-interest] Shutting down ExecutorService

Gregg Wonderly gregg at cytetech.com
Mon Apr 20 17:52:00 EDT 2009


Anytime you have a ServerSocket opened in a thread somewhere, any other thread 
can close that ServerSocket.  This is the most reliable way to intercede and 
wake up a blocked thread that is at ServerSocket.accept().  In general, Java I/O 
has always allowed one thread to close an endpoint that another thread is 
blocked on and wake that thread up.

Gregg Wonderly

Thomas Barnett 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