Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Multi-threaded Java NIO possible?

843790Jan 31 2003 — edited Mar 31 2010
Hi,

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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 28 2010
Added on Jan 31 2003
9 comments
2,847 views