In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of NIO in Java, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
1. Java mind map
II. Ipaw O model
The essence of Icano model is what kind of channel to send and receive data, which determines the performance of program communication to a great extent.
Java supports three network programming models: BIO, NIO, and AIO
BIO: synchronized and blocked, the service implementation pattern is to connect one thread at a time, that is, when the client has a connection request, the server needs to start a thread to process it.
NIO: synchronous non-blocking, the server implementation mode handles multiple request connections for one thread, that is, all requests sent by the client are registered with the multiplexer, and the multiplexer polls the connection and processes it when there is an Imodo request.
AIO: asynchronous non-blocking. AIO introduces the concept of asynchronous channel and adopts Proactor mode, which simplifies programming and starts threads only with valid requests. Its characteristic is that the server is notified after the completion of the operating system.
Third, BIO, NIO, AIO application scenarios
The BIO method is suitable for the architecture with a small number of connections and a fixed number of connections, which requires high server resources, and concurrency is limited to the application. BIO is the only choice in the past, but the program is simple and easy to understand.
NIO is suitable for architectures with a large number of connections and short connections (light operation), such as chat server, on-screen comment system, server-to-server communication, etc. Programming is more complex, and JDK1.4 begins to support it.
AIO is used in architectures with a large number of connections and long connections (re-operation), such as photo album server, fully calling OS to participate in concurrent operations, programming is more complex, JDK7 begins to support
4. Simple process of BIO programming
Start a ServerSocket on the server side
The client starts Socket to communicate with the server. By default, the server needs to set up a thread for each client to communicate with it.
After the client makes a request, it first asks the server whether there is a thread response, and if not, it will wait or be rejected.
If there is a response, the client thread will wait and continue execution after the request is over.
5. NIO core
NIO has three core parts: Selector (selector), Channel (channel), and Buffer (buffer).
NIO is buffer-oriented, or block-oriented programming, where data is read into a buffer it processes later, and can move back and forth in the buffer when needed, which increases the flexibility in processing and provides a non-blocking, highly scalable network.
HTTP2.0 uses the technology of multiplexing to process multiple requests concurrently on the same connection, and the number of concurrent requests is several orders of magnitude larger than that of HTTP1.1.
In short, NIO can handle multiple requests in a single thread.
VI. Comparison between BIO and NIO
BIO processes data in the way of stream, while NIO processes data in block way. The efficiency of block I hand O is much higher than that of stream I hand O.
BIO is blocking and NIO is non-blocking
BIO operates based on byte and character streams, while NIO is based on Channel (channel) and Buffer (buffer) forward operations, and data is always read from the channel into the buffer or written from the buffer to the channel. Selector (selector) is used to listen for events in multiple channels (such as connection requests, data arrival, etc.), so you can listen to multiple client channels using a single thread.
VII. Schematic diagram of the three core principles of NIO
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.