在Java中选择包
我正在尝试用 Java 重写 Python 库。我想知道是否有任何 java 包可用,类似于 Python 中的 select
模块。
http://docs.python.org/library/select.html
我已被推荐nio 包,但我想知道是否有一个稍微更相似的 Java 实现。
I am trying to re-write a Python library in Java. I was wondering if any java package was available which is analogous to the select
module available in Python.
http://docs.python.org/library/select.html
I have been referred to the nio package but I was wondering if there was a slighly more similar Java implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您愿意为 select 编写 JNI 包装器(2 )自己,或者可以在网上找到一个。 (我简单地看了一下,但没有找到。)如果您想支持 Java 的打开文件概念(例如,映射 Java
InputStream 到 Unix 文件描述符中)。
正如您所指出的,在 Java 中执行此操作的首选方法是使用 java.nio。不过,
nio
确实有一些限制。除了 API 差异之外,nio
和 select(2) 之间的最大区别是nio
只会通过网络套接字进行多路复用。例如,您不能使用它来多重选择打开的文件。Not unless you are willing to write a JNI wrapper for select(2) yourself, or can find one on the 'net. (I looked briefly, and I didn't find one.) That's likely to be a fun little challenge, too, if you want to support Java's notion of an open file (e.g., to map a Java
InputStream
into a Unix file descriptor).The preferred way to do this in Java is, as you've noted, with
java.nio
.nio
does have some restrictions, though. The biggest difference betweennio
and select(2), aside from the API differences, is thatnio
will only multiplex over network sockets. You can't use it to multiselect open files, for instance.不确定这是否适合您,但您可以尝试 Jython 而不是用 java 重写它。
Not sure if this will work for you, but you could try Jython instead of re-writing it in java.