title: Try-with-resources and ServerSockets
body: <p>I was reading through the KnockKnock server example at <a href="http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html</a> and I found a bit of code I have a few questions about.</p>
  
  <pre><code>try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
      while (listening) {
          new KKMultiServerThread(serverSocket.accept()).start();
      }
  } catch (IOException e) {
      System.err.println("Could not listen on port " + portNumber);
      System.exit(-1);
  }
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li>What is the scope of <code>serverSocket</code>? Is it usable in the caught exception block, or elsewhere in the surrounding block? If not, how can one reliably close the socket?</li>
  <li>How is the socket closed in this instance? I assume the only way that this sample can stop executing is to forcefully end the process, but what happens to the open socket after this point? Is the port in use no longer available for other applications (or even the same application)?</li>
  <li>What happens to the <code>new KKMultiServerThread</code>? Is this thread cleaned up by the garbage collector once the thread has completed its work?</li>
  </ol>