Multi-threaded Java NIO possible?
843790Jan 31 2003 — edited Mar 31 2010Hi,
I've written a single-threaded Java NIO server, that I'm now trying to make multi-threaded. However, the only approaches I've seen for doing this, are based on two threads: one for accepting connections and one for reading/writing to channels. I tried using a thread pool that I dispatch selected keys to, but I found this approach failed.
My approach now:
- select keys that are ready for operation
- for each key, dispatch operation on it to the thread pool
This approach fails because the next time I select keys, some keys are returned again, because the thread pool has not finished working on them yet.
Example:
- selector returns a key that's ready for accepting a connection
- the key is handed to the thread pool where a thread is waiting
to operate on it
- the server loop finishes and the selector selects keys again
- the same key is returned, because the thread pool has not accepted
the connection yet
Is there a way to write a Java NIO server with more than two threads? I need this for a very high-performance server.
Thanks in advance,
Ronald Wildenberg.