Original Post
After receiving some good advice on these forums, I've been playing with asynchronous socket I/O using java.nio. The problem I have with the examples available all over the place is that they deal properly with input but not output. For instance, Sun's own examples use non-blocking input but use blocking output (using a PrintStream object). That's poor form from Sun. Other examples I've seen use busy loops for the output. That's just wrong. So I've been thinking of how to handle input. When a connection is accepted and its channel is registered with the selector, at first it would make sense to register it as interested in reading and writing. Question #1: If the connection is ready to send, will SelectionKey.isWritable() return true on every select? This seems to be a waste of time if there's nothing to send. Question #2: In a typical game server, would it be practical to keep calling interestOps(int ops) to register interest in writing only when there's something to write, or would that crap out select loop due to the possibility of interestOps blocking? Question #3: Is there a nice (and proven) way of toggling interest in writing that avoids blocking? I was thinking of using a ConcurrentLinkedQueue in its own thread to process interestOps operations. Would this work? Thanks in advance for any advice.