aspose file tools
The moose likes Java in General and the fly likes Handling data from a collection of (persistent) inputstreams in Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Handling data from a collection of (persistent) inputstreams in "real time"" Watch "Handling data from a collection of (persistent) inputstreams in "real time"" New topic
Author

Handling data from a collection of (persistent) inputstreams in "real time"

Alan Su
Greenhorn

Joined: Jul 11, 2004
Posts: 2
Hi, I am just writing a little project for fun. Basically its like a game that allows many ppl/players to connect. So I have a collection of socket connections and their input/output streams. I am not sure how I should design the part for reading from these input streams. For now all I can think of is either make a thread for each connection, wait till all the information i need is received through blocking read, then send it to the "engine". This could be bad if I ever have lots of players because lots of threads are created. Or, I can have non blocking read, and have the server poll every connection every .2 seconds. But polling is again ugly. So which way is better? or is there any alternatives? maybe like without using so many threads and allow the OS to send an event to notify the engine whenever new data arrive, so I don't need to poll.

Thank you very much.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24051
    
  13

The traditional way to do this was to use a thread per input stream. But starting in JDK 1.4, there's the java.nio package, which includes a "select"-like functionality that lets you wait for input to appear on any of a collection of input channels. Here is a resonable place to start learning about NIO.


[Jess in Action][AskingGoodQuestions]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

what do you think are lots of threads are created ?


http://home.arcor.de/hirnstrom/bewerbung
Alan Su
Greenhorn

Joined: Jul 11, 2004
Posts: 2
Cool, thanks so much, that seems to work in my case .

Well, if there are 100 players, there will be hundred thread (just planning ahead if somehow I get that many ); I just thought that would be over use of thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling data from a collection of (persistent) inputstreams in "real time"
 
Similar Threads
What is non blocking in nio
Unblocking sockets
NIO with Servlet 3.0 for Streaming Comet
chat
How to write a lobby game server