简体   繁体   English

Java非阻塞io可以用于所描述的应用程序吗?

[英]Can Java non-blocking io be used for the described application?

I had built a java TCPServer using serversocketchannels running on one port. 我使用在一个端口上运行的serversocketchannels构建了一个java TCPServer。 However, it is not very scalable as it attends to one incoming socket (blocking mode) only. 但是,由于它仅涉及一个传入套接字(阻塞模式),因此扩展性不是很高。

I want to extend this TCPServer to service multiple incoming sockets (maximum 10 incoming sockets). 我想将此TCPServer扩展为服务多个传入套接字(最多10个传入套接字)。 As such, am wondering if i should implement the TCPServer using non-blocking io or use thread+blocking io. 因此,我想知道我应该使用非阻塞io还是使用线程+阻塞io来实现TCPServer。

Paul Tyma recently compared the two approaches, generating diverse discussion . 保罗·泰玛(Paul Tyma)最近比较了这两种方法,引起了广泛的 讨论 Under certain circumstances, modern threading libraries can outperform java.nio.channels.Selector . 在某些情况下,现代线程库的性能可能优于java.nio.channels.Selector As the result is somewhat counter-intuitive, you may have to prototype both to get a definitive answer. 由于结果有些违反直觉,因此您可能必须对两者都进行原型设计才能获得确定的答案。

JBoss-Netty or Apache-Mina are nio framework that provide much things to implement your own server. JBoss-Netty或Apache-Mina是nio框架,它们提供了很多东西来实现您自己的服务器。 So, now i'm using netty and happy with it. 所以,现在我正在使用netty并对此感到满意。

With only 10 incoming sockets I don't think the effect is clear to see. 只有10个传入的套接字,我认为效果不明显。 What you should do is to focus on the upper layer (protocol, application) and leave that low level network implementation to a framework. 您应该做的是专注于上层(协议,应用程序),并将低层网络实现留给框架。 I would recommend the Apache Mina for that job. 我会推荐该职位的Apache Mina As you will see, I does the job very well with blocking or non blocking, you choose; 正如您将看到的,您选择了阻塞或不阻塞,我的工作做得很好。 and leave open interfaces for you to implement the protocol & application. 并保留开放的接口供您实施协议和应用程序。

I would use threads and blocking I/O until you know you have at least 1000 concurrent connections. 我将使用线程并阻止I / O,直到您知道至少有1000个并发连接。 That also gives you an easy way to get it working. 这也为您提供了使其工作的简便方法。 When and if you get to 1000, evaluate. 如果达到1000,则进行评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM