Search code examples
javapollingserversocket

Java poll on network connections


I am writing a program in Java where I have opened 256 network connections on one thread. Whenever there is any data on a socket, I should read it and process the same. Currently, I am using the following approach :

while true
do
   iterate over all network connections
   do
         if non-blocking read on socket says there is data
         then
               read data
               process it
         endif
   done
   sleep for 10 milli-seconds
done

Is there a better way to do the same on Java ?? I know there is a poll method in C/C++. But after googling for it, I did not get concrete idea about Java's polling. Can somebody explain this ??


Solution

  • The java.nio package sounds right for what you want to do. It provides ways to perform asynchronous IO.