title: Java thread pooled server using blocking I/O
body: <p>I have implemented a server in Java, upon receiving data from some client it simply forwards the data to all other clients (including the sender). I'm happy with my OO-design, I wrap all sockets in classes that provide 'callbacks'. These are called when some data are ready (or when the socket closes) -- using this design I could easily implement a simple TLV protocol to atomically send packets: the callback is not called until a full packet is received.</p>
  
  <p>Now, I use the <code>java.io</code> package blocking I/O calls to the socket streams (and make them appear 'asynchronous' through those callbacks). So I use threads inside my socket wrapper classes: when a socket is opened, that function returns a <code>Runnable</code> implementation that, when run, will do the blocking calls to the <code>InputStream</code>, buffer data and eventually call the callback.</p>
  
  <p>=> In a client application, I simply launch this <code>Runnable</code> in a <code>Thread</code> instance, because it's just one thread.</p>
  
  <p>=> In my server, I submit all <code>Runnable</code> implementations I get upon creating new sockets (i. e. when accepting new clients) into a <code>ThreadPoolExecutor</code>. (FYI: the callbacks of the sockets simply <code>put</code> the received packets in a <code>BlockingQueue</code>. A single, separate (non-pooled) "dispatcher" <code>Thread</code> instance constantly <code>take</code>s the packets from this queue and writes them to all sockets currently connected to the server.)</p>
  
  <p><strong>QUESTION:</strong> This all works great, however I'm unsure about my use of the <code>ThreadPoolExecutor</code>, because the <code>Runnable</code> instances submitted are almost always blocking. Will the <code>ThreadPoolExecutor</code> react to this? Or will the pooled threads simply block? Because, if all pooled threads are all blocking while executing their <code>Runnable</code> and next, a new <code>Runnable</code> is submitted, then what? Suspend the new <code>Runnable</code>? That's not good, because then the newly connected client will have zero responsiveness until some older client disconnects. If by contrast the thread pool chooses to spawn a new thread to handle the <code>Runnable</code>, then I actually get a thread-per-client scenario.</p>
  
  <p>I want the thread pool to 'preempt' the blocking threads and use them to handle other sockets, like an operating system that suspends I/O bound processes and doesn't schedule them again until their I/O is complete. Is that at all possible, or will I have to rewrite everything using <code>nio</code> in order to do this? (if <code>nio</code> is required, could you point out where I should start reading?)</p>
  
  <p>Thanks in advance!</p>