The difference and comparison between Java NiO and IO

Source: Internet
Author: User

There are two different ways of modern hotel service than the traditional one:
1, added a role, to have a special responsibility to collect the guests needs of the people. What corresponds to NiO is selector.
2, from the blocking service to non-blocking service, the guests eat when the waiter does not have to be waiting for guests next. Traditional IO operations, such as read (), are blocked until the data arrives when there is no data to read. When no data is readable in NiO, read () returns 0 immediately and the thread does not block.
In NiO, after the client creates a connection, the connection is registered to selector, the equivalent of the guest entering the restaurant, telling the front desk that you want to dine, the front desk will tell you what the number is, and then you may be seated at that table, Selectionkey is the table number. When a table needs to serve, the front desk will record which table needs what service, such as table 1th key dishes, table 2nd to check out, the waiter from the front desk to take a record, according to the records to provide services, finished again to remove a. So the time of service is used most effectively.

Difference:
IO NIO
Stream-oriented buffering
Blocking IO non-blocking IO
No selector

    

Java NiO provides a different way of working with iOS than standard IO:
Channels and buffers (channels and buffers): Standard IO is based on byte stream and character stream, while NiO is based on channel (channels) and buffers (buffer), data is always read from the channel into the buffer, or written to the channel from the buffer.
Asynchronous IO (Asynchronous IO): Java NIO allows you to use IO asynchronously, for example: When a thread reads data from a channel to a buffer, the thread can do something else. When the data is written to the buffer, the thread can continue to process it. Writing a channel from a buffer is similar.
Selectors (selector): Java NiO introduces the concept of selectors, which are used to listen for events on multiple channels (for example: Connection open, data arrival). Therefore, a single thread can listen on multiple data channels.

Usage scenarios:
Advantages of NiO:
1. The advantage is that one thread manages multiple channels, but the processing of the data becomes complex;
2. If you need to manage thousands of simultaneous open connections, these connections send only a small amount of data at a time, using this;
Advantages of traditional IO:
1. Applies to a thread to manage a channel, because the stream data read is blocked;
2. If you need to manage to open not too many connections at the same time, these connections will send a large amount of data;

Difference:
The difference between NiO vs IO concept above (NIO will block to the background thread execution):

IO是面向流的,NIO是面向缓冲区的Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方;NIO则能前后移动流中的数据,因为是面向缓冲区的IO流是阻塞的,NIO流是不阻塞的Java IO的各种流是阻塞的。这意味着,当一个线程调用read() 或 write()时,该线程被阻塞,直到有一些数据被读取,或数据完全写入。该线程在此期间不能再干任何事情了Java NIO的非阻塞模式,使一个线程从某通道发送请求读取数据,但是它仅能得到目前可用的数据,如果目前没有数据可用时,就什么都不会获取。NIO可让您只使用一个(或几个)单线程管理多个通道(网络连接或文件),但付出的代价是解析数据可能会比从一个阻塞流中读取数据更复杂。 非阻塞写也是如此。一个线程请求写入一些数据到某通道,但不需要等待它完全写入,这个线程同时可以去做别的事情。选择器Java NIO的选择器允许一个单独的线程来监视多个输入通道,你可以注册多个通道使用一个选择器,然后使用一个单独的线程来“选择”通道:这些通道里已经有可以处理的输入,或者选择已准备写入的通道。这种选择机制,使得一个单独的线程很容易来管理多个通道。*参考博客分析:**http://blog.csdn.net/evanman/article/details/50910542https://www.cnblogs.com/kzfy/p/5063467.html*                

The difference and comparison between Java NiO and IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.