与Java中的传统IO相比,NIO性能改进 [英] NIO Performance Improvement compared to traditional IO in Java

查看:147
本文介绍了与Java中的传统IO相比,NIO性能改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过许多文章/博客说与传统的Java IO相比,Java NIO是一个更好的解决方案。

I have seen many articles/blogs saying that Java NIO is a better solution compared to traditional Java IO.

但今天我的一位同事向我展示了这个博客 http:// mailinator。 blogspot.com/2008/02/kill-myth-please-nio-is-not-faster-than.html 。我想知道Java社区中是否有人做过与Java NIO性能相关的这种基准测试。

But today one of my co-worker showed me this blog http://mailinator.blogspot.com/2008/02/kill-myth-please-nio-is-not-faster-than.html. I am wondering whether anyone from the Java community has done this kind of benchmarking related to Java NIO performance.

推荐答案

NIO vs IO是一个非常有趣的话题要讨论。

NIO vs IO is a pretty fun topic to discuss.

根据我的经验,这两个是两个不同的工具,分别用于两个不同的工作。我听说IO被称为每个客户的线程方法,而NIO被称为所有客户的一个线程方法,我发现这些名称虽然不是100%准确,但还不够合适。

It's been my experience that the two are two different tools for two different jobs. I have heard of IO being referred to as the 'Thread per Client' approach and NIO as the 'One thread for all Clients' approach and I find the names, while not 100% accurate, to be fitting enough.

正如我所看到的,NIO和IO的真正问题在于可扩展性。

The real issue with NIO and IO, as I see it, is with scalability.

NIO网络层将(应该?)使用单个线程来处理选择器并将读/写/接受作业分派给其他线程。这允许处理选择器的线程(选择器线程)除此之外什么都不做。这样可以在处理大量客户(请注意缺少实际数量)时更快地响应。现在,NIO开始崩溃的时候是服务器获得如此多的读/写/接受,而选择器线程一直在工作。过去这个和服务器的任何其他工作都开始滞后。此外,由于所有读/写/接受作业都由选择器线程处理,因此在混合中添加额外的CPU不会提高性能。

An NIO network layer will (should?) use a single thread to handle the selector and dispatch read/write/accept jobs to other thread(s). This allows the thread handling the selector (the 'Selector Thread') to do nothing but just that. This allows for much faster response when dealing with a lot (note the lack of actual numbers) of clients. Now, where NIO starts to fall apart is when the server is getting so many read/write/accept that the Selector Thread is constantly working. Any additional jobs past this and the server starts to lag. Additionally, since all read/write/accept jobs are processed by the Selector Thread, adding additional CPUs to the mix will not improve performance.

IO网络层可能会占用每个套接字1个线程的方法,包括监听套接字。因此线程数与客户端数量成正比。在适量的客户下,这种方法非常有效。通过使用这种方法支付的成本以线程成本的形式出现。如果您有2000个客户端连接到服务器...您至少有2001个线程。即使在四芯片中,每个芯片机器有6个内核,你只需要24个处理节点(如果算上超线程就有48个)来处理这些2001线程。实例化所有这些线程会花费cpu time和ram,但即使你使用线程池,你仍然需要支付CPU上下文切换的成本,因为它们从一个线程移动到线程。这可能会在高服务器负载下变得非常难看,如果编码不正确,可能会使整个机器停止运行。从好的方面来说,在这种情况下,将CPU添加到机器将提高性能。

An IO network layer will likely take the approach of 1 thread per socket, including the listening socket. So the number of threads is directly proportional to the number of clients. Under a moderate amount of clients, this approach works really well. The cost that one pays by using this approach comes in the form of the cost of a thread. if you have 2000 clients attached to a server... you have at least 2001 threads. Even in a quad chip, 6 core per chip machine, you only have 24 processing nodes (48 if you count HyperThreading) to handle those 2001 threads. Instantiating all those Threads costs cpu time and ram, but even if you use Thread Pooling, you still have to pay the cost of the CPUs context switching as they move from thread to thread. This can get very ugly at high server loads, and, if not coded properly, can grind the entire machine to a halt. On the plus side, adding CPUs to the machine WILL improve performance in this case.

现在一切都很好,但是因为没有数字我的描述是为了帮助决定使用IO或NIO。这是因为还有更多变量需要考虑:

Now all that is well and good, but is in the abstract because there's no numbers in my description to aid in making a decision to go with IO or NIO. This is because there's even more variables to consider:


  • 客户的生命周期?短期还是长期?

  • 每个客户预计的数据量?很多小块或几块巨大的块?

  • 预计会同时连接多少个客户端?

  • 你在运行什么操作系统和什么是JVM你正在用吗?这两个因素都包括线程和投票费用。

  • Life time of a client? Short or long?
  • Amount of data expected per client? Lots of small chunks or few huge chunks?
  • Just how many clients are expected to be connected simultaneously?
  • What OS are you on and what JVM are you using? Both factor into Thread and polling costs.

只是一些值得思考的问题。要回答哪个问题更快,NIO或IO:两者兼而有之::)

Just some food for thought. To answer the question of which is faster, NIO or IO: Both and neither :)

这篇关于与Java中的传统IO相比,NIO性能改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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