java - 为什么NIO的性能比IO好?
阿神
阿神 2017-04-17 17:39:32
0
5
603

我目前已知的原因只有这1个: 就是由于NIO采用缓冲区+通道的方式来传输数据, 而不是IO包的以字节或字符为单元传输数据, 增加了一次处理数据的字节数, 而且这种方式更接近于底层操作系统的IO方式, 所以速度明显优于IO;

除了这一点还有其他的吗?

阿神
阿神

闭关修行中......

reply all(5)
迷茫

0zhangzhun132·Just

1.io is stream-oriented, that is, when reading data, it is read from the stream one by one, so the data cannot be viewed as a whole and there is no buffer; nio is buffer-oriented, and the data is stored in the buffer. Reading data is performed in the buffer, so it is more convenient to perform data offset operations
2. io is blocked. When a thread operates io, if there is currently no data to read, the thread is blocked, and nio is a channel operation. io, so it is non-blocking. When a channel has no data to read, the channel can be switched to process other io
3. Nio has a selector, which means that threads can select multiple channels through the selector, while io can only process one

Purely done by hand, I hope it helps you

EOF

洪涛

Assume the following scenario: on a 4-core machine, receive network requests, process business logic, and return the processing results to the client. The network requests are 10,000/s. During business logic processing, there are The operation of importing files requires blocking for 5 seconds of processing time, assuming that the transfer does not take time.

1. Use BIO processing:

Due to the long IO time in business logic processing, the processing thread will be blocked. In the case of multiple requests, one request will generate a thread to process the business, so within 5 seconds, 50,000 threads need to exist in the system. Used to handle business logic. The CPU will use most of its resources to handle thread scheduling.

2. Use NIO processing:
The IO operations in NIO are asynchronous and will not block the business logic thread. In the case of multiple requests, 4 business threads are opened, one thread occupies one CPU core, and the business thread can be processed at full speed. business logic. For IO operations in the business, they can be processed asynchronously in the hardware, and messages are sent to the CPU for subsequent business processing when the data is ready. At this time, only four threads are needed to process the business efficiently, and the CPU is almost not idle. There is no need to consume resources in thread scheduling.

刘奇

Whether it is IO or NIO, it is a manifestation of the Linux network I/O model. It is recommended to learn the Linux network model.

Ty80

1.nio does memory mapping, eliminating one copy between user space and system space
2.nio is asynchronous, triggered response, non-blocking response, making full use of system resources, mainly CPU

巴扎黑

<h1>Hey hey</h1>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template