how to choose java nio vs io?

前端 未结 7 1433
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 07:12

As we had known, If we want to use traditional IO to construct server, it must block somewhere, so we had to use loop or one thread one socket mode, So nio seem it is better

相关标签:
7条回答
  • 2020-12-24 07:39

    A little late, but personally, I use NIO even for the regular "everyday" file handling. So, I use things like:

     1. if(Files.notExists(path)) { } 
     2. Files.createDirectory(path);
     3. Files.newInputStream(path) targetPath.resolve("somefile.txt");
     4. Files.newBufferedWriter(path, charset);
     5. DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
    

    and it works fine for me. I prefer Path instead of the old File because of methods like relativize or resolveSibling.

    Doesn't strike me as more complicated than IO.

    0 讨论(0)
  • 2020-12-24 07:40

    You can use any of this, unless you are going to create "super fast" server.

    Of course a good approach here is to use nio, since it's new and modern way to write multi-client servers for high throughput tasks.

    0 讨论(0)
  • 2020-12-24 07:43

    You would only use NIO if you can justify the inevitable complexity that it introduces. If you do not have any guidance in terms of the expected load, and also in terms of whether your product / project has the resources to maintain the relevant code, then your should err on the side of caution and use IO.

    To give my answer some weight, I have just spent three months maintaining and bug fixing an integration layer where raw Java NIO (i.e. no overarching framework was used) was used. The design was based, in essence, on client threads adding messages to a queue and a small number of worker threads performing their NIO magic and then passing replies back to client threads in an event-based manner. In retrospect, I cannot justify the original decision to use NIO, since it became a distraction that ate significant amounts of time that should have been spent on higher level business logic.

    0 讨论(0)
  • 2020-12-24 07:44

    Traditional IO is easy and simplified code, NIO is more complicated but more flexible. In my case i prefer use IO for small streaming and NIO for large streaming but nio is really more complex

    with NIO i have to create an entire package to manage it instead io package that i directly use snippet

    0 讨论(0)
  • 2020-12-24 07:50

    If you want non-blocking IO, NIO is not the better choice—it's the only choice in Java. Keep in mind that people still use the old IO regularly because it is way simpler to code against. NIO API is quite raw and is more of an enabling low-level technology than a client-side API. I suggest using NIO through an API that provides a simpler interface to the problems you want to solve using non-blocking IO.

    0 讨论(0)
  • 2020-12-24 07:58

    IMHO, Blocking IO is generally the simplest to use, and unless you have a specific requirement which demands more from your system, you should stick with simplest option.

    The next simplest option is blocking NIO, which I often prefer if I want something more efficiency or control than IO. It is still relatively simple but allows you to use ByteBuffers. e.g. ByteBuffers support little endian.

    A common option is to use non-blocking NIO with Selectors. Much of the complexity this introduces can be handled by frameworks such as Netty or Mina. I suggest you use such a library if you need non-blocking IO e.g. because you have thousands of concurrent connections per server. IMHO You have thousands of connections, you should consider having more servers unless what each connection does is pretty trivial. AFAIK google go for more servers rather thousands of users per server.

    The more extreme option is to use NIO2. This is even more complex and lengthy it write than non-blocking NIO. I don't know of any frameworks which support this well. i.e. it is actually faster when you do. AFAIK It appears this is worth using if you have Infiniband (which is what it was designed to support) but perhaps not worth using if you have Ethernet.

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