Java NIO,使用还是不使用框架?
我正在开发一个基于 Java 的服务器,带有 NIO 多路复用,我开始看到很多框架...我不明白这些框架是否只是让生活变得更轻松,或者还提高了性能(例如 netty )
I'm developing a Java Based server, with NIO multiplex and I started to see a lot of frameworks... I don't understand if these frameworks makes the life easier only or has also an increment of performance ( for example netty )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有任何框架可以提高其底层的性能。就 NIO 而言,我开始认为它本身已经是一个框架了。我已经审查了几个 NIO 框架,例如 Mina,并且确实自己编写了一个框架,但我自己的结论是,这很大程度上是浪费精力,最终会以某种方式阻碍。您所需要的只是一个编写良好的选择循环和适当的数据结构。
No framework can increase performance of what's underneath it. In the case of NIO I've come around to the view that it already is a framework itself. I've reviewed a couple of NIO frameworks such as Mina, and indeed wrote one myself, but my own conclusion is that this is largely wasted effort, that ultimately gets in the way one way or another. All you need is a well-written select loop and the appropriate data structures.
我认为核心点是它们让生活更轻松/让你更快地提高工作效率。与彼此相比,或者与您自己的代码相比,它们的性能可能或多或少(没有理由认为如果您从头开始编码,第一次尝试就会获得更好的性能 - 当然最终您拥有它,因此您可以将其优化到如果你愿意并且有时间的话,可以去死)。
最终他们都使用 Java NIO 框架和类,而超越这些框架和类的唯一方法是做你自己的 JNI - 假设你成功了 - 这是很难的东西,实际上是编程中的一个专业。
I think the core point is that they make life easier/get you productive faster. They may be more or less performant compared to each other, or to your own code (no reason to think that if you coded it from scratch you would get better performance the first try - of course ultimately you own it so you can optimize it to death if you want and have the time).
Ultimately they are all using the Java NIO framework and classes, and the only way to outperform those is to do your own JNI - assuming you succeeded - it is hard stuff, really a specialty of its own within programming.
这取决于您想要做什么。 NIO 框架很有用,因为它们为您提供了 NIO 核心操作的抽象。尽管如此,它们迫使您使用几种您可能不习惯的设计模式。
如果您认为自己能够适应这些设计模式,那么您可能应该使用框架。它将有更少的错误,您要做的工作也更少,最终您将看不到所有操作发生的位置。您只需专注于您想要实现的目标即可。
与“国内”解决方案相比,它有一些额外的开销,但可以忽略不计。
It depends on what you're trying to do. NIO frameworks are useful because they provide you an abstraction of NIO core action. Although, they force you to use several design patterns you may not be comfortable with.
If you think you adapt yourself to those design patterns you should probably use a framework. It will have less bugs, you will have less work to do and ultimately you won't see where all of the action happens. You just have to focus on what you are trying to achieve.
It has some additional overhead in comparison to a "domestic" solution but it is negligible.
这实际上取决于您对 java.nio API 的了解程度。如果您不确定事情是如何工作的,那么您可能应该使用第 3 方 API。如果您知道事情是如何工作的并且能够在没有第 3 方 API 的情况下编写代码,那么您绝对应该使用自己的代码而不附加任何条件。您无需额外的操作(第 3 方 API)即可获得更好的性能。
我喜欢遵循KISS 原则。
It really depends on your level of knowledge with the
java.nio
API. If you're not sure on how things work then you should probably use a 3rd party API. If you know how things work and are capable of writing code without a 3rd party API then you should definitely use your own code without any strings attached. You can achieve better performance without extra things (3rd party API) going on.I like to live by the KISS principle.