网络连接上的Java轮询 [英] Java poll on network connections

查看:87
本文介绍了网络连接上的Java轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写程序,其中我在一个线程上打开了256个网络连接.每当套接字上有任何数据时,我都应该读取并进行处理.目前,我正在使用以下方法:

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

在Java上是否有更好的方法可以做到这一点?我知道C/C ++中有一种轮询方法.但是在搜索之后,我对Java的轮询并没有具体的想法.有人可以解释一下吗?

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 ??

推荐答案

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

这篇关于网络连接上的Java轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆