Asycronous IO in java ? ? ?
Main Topics
Browse All TopicsHi all
I need to have a server managing allot of connection in simultaneous. Having a thread for each connection is out of question.
So how do I do this in just one Thread managing all the connections?
Something like the POSIX select command
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
In my experience, if you want something like select, you'll have to write it yourself. Have a master thread controlling incoming connections and a multiplexing controller that polls connectiong for read/write ready states.
You might find this page useful: http://www.developer.com/n
You don't have to sleep with the multiplexor concept, you can ahve the multiplexor constantly going through a list of connections polling them.
You can have a separate pair of threads for read and write respectively. NB: this requires some shared memory vodoo!
e.g.
// multiplexor
for(int i = 0; i < connections.length; i++){
//poll connection[i] for read
readyToRead[i] = true;
//poll connection[i] for write
readyToWrite[i] = true;
}
//read loop
for(int i = 0; i < connections.length; i++){
if (readyToRead[i])
// do read action
}
//write loop
for(int i = 0; i < connections.length; i++){
if (readyToWrite[i])
// do write action
}
true - it does use CPU continuously, the only fix to that would be to add a delay to the multiplexor and have it signal when action needs to be taken. Use signal listerners in each of the children's threads so that they may sleep when nothing needs doing. But point well taken, the solution I offered uses up CPU needlessly.
You can look at non-blocking IO
http://www.exampledepot.co
Have you looked into the Grizzly server?
https://grizzly.dev.java.n
Grizzly 2.0 is out now, and some examples are coming out:
http://blogs.sun.com/oleks
Business Accounts
Answer for Membership
by: CEHJPosted on 2008-11-14 at 10:05:43ID: 22961938
Even two connections on the same thread is too many, since any problem in just one of the connections can crash or block them all. Any serious application will therefore use one thread per connection