I have seen many articles/blogs saying that Java NIO is a better solution compared to traditional Java IO.
But today one of my co-worker showed me this blog http://m
NIO is used not because it's faster but because it has better scalability especially there are amounts of clients.
IO (Blocking IO/Stream IO) is usually one thread per connection to get better response to the clients. Suppose you use single thread to (blocking)listen/(blocking)read/process/(blocking)write for all the clients, just like Starbucks serves all the customers in a single window, Starbucks customers (your clients) would get impatient (timeout).
Note you may think about thread pool to avoid huge number of threads drag down your server. While it just like Starbucks lines all the customers into several windows, the customers are still delay because of other's blocking. So that's why one thread per connection is a good choice in tradition java IO programming.
NIO (None Blocking IO/Block IO which one to use) uses Reactor Pattern to handle IO events. In this case, you could use single thread to blocking/listen|read|process|write. Then the clients blocking (waiting period) would not affect each other.
Note both IO and NIO can use multiply-threads to utilize more cpu-resources, more details in Doug lee's introduction.
Java NIO is considered to be faster than regular IO because:
Java NIO supports non-blocking mode. Non-blocking IO is faster than blocking IO because it does not require a dedicated thread per connection. This can significantly improve scalability when you need to handle lots of simultaneous connections, as threads are not very scalable.
Java NIO reduces data copying by supporting direct memory buffers. It is possible to read and write NIO sockets without any data copying at all. With traditional Java IO, the data is copied multiple times between the socket buffers and byte arrays.
Java IO encomapsses several constructs and classes. You cannot compare on such general level. Specifically , NIO uses memory mapped files for reading - This is theoretically expected to be slightly faster than a simple BufferedInputStream
file reading. However , if you compare something like a RandomAccess
file read , then NIO memory mapped file will be a lot faster.
Also, AFAIK, Java IO was rewritten to use NIO under-the-covers (and NIO has more functionality). Microbenchmarks are just a bad idea, particularly when they're old, as lavinio states.
Java NIO and the reactor pattern are not much about networking performance itself, but about the advantages that the single-threaded model can deliver to a system in terms of performance and simplicity. And that, the single-threaded approach, can lead to dramatic improvements. Take a look here: Inter-socket communication with less than 2 microseconds latency
The article you cite is three years old. It used Java 1.4.2 (4).
Since then Java 5, 6, and now 7 are out.
Huge changes inside of the JVM as well as the class library have rendered anything having to do with benchmarking of 1.4.2 irrelevant.
If you begin to dig, you'll also note that the distinction between java.io and java.nio isn't quite so clear. Many of the java.io calls now resolve into java.nio classes.
But whenever you want increased performance, the solution is not to just do any one thing. The only way to know for sure is to try different techniques and measure them, because what's fast for my application isn't necessarily so for your application, and vice-versa. NIO might well be slower for some applications. Or it might be the solution to performance problems. Most likely, it's a little of both.