title: Java sockets - asynchronous wait, synchronous read
body: <p>I want to be able to asynchronously wait on a socket, then synchronously read from it:</p>
  
  <pre><code>for (;;) 
  {
      while (data available on socket) 
      {
          read message from socket;
          process it;
      }
      do something else;
  }
  </code></pre>
  
  <p>I need this because I want to poll a queue with messages from GUI at the same time, so the "do something else" part has a short <code>wait()</code>.</p>
  
  <p>Is this possible with Java sockets? I tried to check <code>.available()</code> on a <code>DataInputStream</code>associated with the socket, but</p>
  
  <ul>
  <li>It seems to work only when I connect, not when I accept a connection (???)</li>
  <li>I get no indication that the connection has been closed.</li>
  </ul>
  
  <p>I tried to do it with a Selector, but it requires the socket channel to be in non-blocking mode all the time.</p>