title: Reliably closing client sockets in a thread pool on shutdown
body: <p>I have a simple ServerSocket-based java program which accepts a client connection and puts it in a threadpool (ThreadPoolExecutor):</p>
  
  <pre><code>while() {
      socket = serverSocket.accept();
      pool.submit(new Client(socket)); // Client is a Runnable
  }
  </code></pre>
  
  <p>Problem is when I try to shutdown the program and client threads stay around until the connection is closed on the other end (the jvm does not exit until then).</p>
  
  <p>I could try to maintain all client objects and close all their sockets when server is about to shutdown. But I have no clue how maintain them? Especially removing them when done.</p>
  
  <p>I tried <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html#beforeExecute(java.lang.Thread,%20java.lang.Runnable)" rel="nofollow">beforeExecute</a> etc, but that only gives me another object (a FutureTask), not the Client object I'm looking for.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>The problem is not adding sockets to a list, the problem is removing them when they are done as there are no way (afaik) to use afterExecute for that.</p>