我正在用 Java 编写一个程序,我在一个线程上打开了 256 个网络连接。每当套接字上有任何数据时,我都应该读取它并进行处理。目前,我正在使用以下方法:
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++ 中有一个 poll 方法。但是在谷歌搜索之后,我并没有得到关于 Java 轮询的具体概念。有人可以解释一下吗??
最佳答案
java.nio包听起来适合你想做的事。它提供了执行异步 IO 的方法。
关于网络连接上的 Java 轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493272/