Java in 2011: threaded sockets VS NIO: what to choose on 64bit OS and latest Java version?

前端 未结 5 1220
囚心锁ツ
囚心锁ツ 2020-12-23 18:46

I\'ve read several posts about java.net vs java.nio here on StackOverflow and on some blogs. But I still cannot catch an idea of when should one prefer NIO over threaded soc

相关标签:
5条回答
  • 2020-12-23 18:54

    What most often is overlooked is that NIO allows zero copy handling. E.g. if you listen to the same multicast traffic from within multiple processes using old school sockets on one single server, any multicast packet is copied from the network/kernel buffer to each listening application. So if you build a GRID of e.g. 20 processes, you get memory bandwidth issues. With nio you can examine the incoming buffer without having to copy it to application space. The process then copies only parts of the incoming traffic it is interested in.

    another application example: see http://www.ibm.com/developerworks/java/library/j-zerocopy/ for an example.

    0 讨论(0)
  • 2020-12-23 19:07

    There is not a single "best" way to build NIO servers, but the preponderance of this particular question on SO suggests that people think there is! Your question summarizes the use cases that are suited to both options well enough to help you make the decision that is right for you.

    Also, hybrid solutions are possible too! You could hand the channel off to threads when they are going to do something worthy of their expense, and stick to NIO when it is better.

    0 讨论(0)
  • 2020-12-23 19:13

    I still think the context switch overhead for the threads in traditional IO is significant. At a high level, you only gain performance using multiple threads if they won't contend for the same resources as much, or they spend time much higher than the context switch overhead on the resources. The reason for bringing this up, is with new storage technologies like SSD, your threads come back to contend on the CPU much quicker

    0 讨论(0)
  • 2020-12-23 19:13

    I would say start with thread-per-connection and adapt from there if you run into problems.

    If you really need to handle a million connections you should consider writing (or finding) a simple request broker in C (or whatever) that will use far less memory per connection than any java implementation can. The broker can receive requests asynchronously and queue them to backend workers written in your language of choice.

    The backends thus only need a thread per active request, and you can just have a fixed number of them so the memory and database use is predetermined to some degree. When large numbers of requests are running in parallel the requests are made to wait a bit longer.

    Thus I think you should never have to resort to NIO select channels or asynchronous I/O (NIO 2) on 64-bit systems. The thread-per-connection model works well enough and you can do your scaling to "tens or hundreds of thousands" of connections using some more appropriate low-level technology.

    It is always helpful to avoid premature optimization (i.e. writing NIO code before you really have massive numbers of connections coming in) and don't reinvent the wheel (Jetty, nginx, etc.) if possible.

    0 讨论(0)
  • 2020-12-23 19:18

    That seems right to me, except for the part about Java limiting the number of threads – that is typically limited by the OS it's running on (see How many threads can a Java VM support? and Can't get past 2542 Threads in Java on 4GB iMac OSX 10.6.3 Snow Leopard (32bit)).

    To reach that many threads you'll probably need to adjust the stack size of the JVM.

    0 讨论(0)
提交回复
热议问题