Java Io:bio and NiO differences and their respective application scenarios

Source: Internet
Author: User

Reprint Please specify source: Jiq Technical Blog-Jiyichin


Introduction

Bio and NIO are two different network communication models, now NIO has been widely used in jetty, ZooKeeper, Netty and other open source frameworks.


A flow-oriented, a buffer-oriented

One is a plug-in, a non-clogging

A no IO multiplexer, a

Here's a sample explanation of the difference:

Assume that the current service-side program needs to read data from a connection established with multiple clients at the same time.

using Bio

Assume the use of plug-in IO. single thread case. The processor thread may be stuck in the read on one of the sockets, resulting in a socket that cannot be processed even when the data is ready. This time the solution is for each socket. Creates a new thread to process its data read.

So, in the bio mode, the service-side program wants to handle multiple sockets at the same time, while waiting for the main thread to receive the connection request, and assigning a new thread to each established connection for processing.

using NiO

Polling method

Assuming that the socket read operation is non-clogging, only one thread will be able to handle the socket at the same time, checking one socket at a time. There is data to read, no then check next. Due to non-clogging. So when you run the read operation, it returns immediately if no data is ready. No clogging occurs.

I/O multiplexing

The disadvantage of this polling method is that it wastes CPU resources. Most of the time may be a lot of readable , without the need to continue to run the read operation repeatedly, I/O multiplexing (iomultiplexing) is a better approach. When the Select function is called. The inside of it maintains a list of listening sockets that are blocked until one of the sockets has data ready to return. and tells you which socket is readable, it is more efficient to call the read function of the socket.


So the basic can feel "NIO = I/O multiplexing + non-clogging I/O", most of the time is single-threaded. But there are more than one thread that implements the NIO situation

NIO three types of models

It is only one single-threaded model, and it is a more extreme case that only one thread is required to handle multiple sockets at the same time. NiO mainly consists of three threading models:

1) reactor single thread model

2) Reactor multithreading model

3) Master-slave reactor multithreading model

Reactor single threading Model:

A single thread completes all things including receiving a TCP connection request from the client, reading and writing socket data, and so on.

For some small-capacity scenarios. Ability to use single-threaded models. But for high-load, large concurrent applications are not appropriate. The main reasons are as follows:

1) A NIO thread handles hundreds and thousands of links at the same time, unable to support performance, even if the CPU load of the NIO thread reaches 100%, it cannot satisfy the encoding, decoding, reading and sending of mass messages;

2) When the NIO thread is overloaded. Processing speed slows down, which causes a large number of client connections to time out and is often re-sent after timeouts. This adds to the load of the NIO thread, which eventually leads to a lot of message backlogs and processing timeouts, and the NIO thread becomes a performance bottleneck for the system;

3) Reliability issues: Once the NIO thread flies unexpectedly, or enters the dead loop, it causes the entire system communication module to be unavailable. Cannot receive and process external messages, resulting in node failure.

In order to solve these problems. The reactor multithreaded model has evolved.

Reactor multithreaded Model:

The biggest difference between the Rector multithreaded model and the single-threaded model is that there is a set of NIO threads that handle real IO operations.

Features of the reactor multithreaded model:

1) There is a special NIO thread-acceptor thread is used to listen to the server, receiving the client TCP connection request;

2) Network IO Operations-read, write, etc. are the responsibility of a NIO thread pool, which can be implemented using standard JDK thread pools. It consists of a task queue and n available threads, which are responsible for reading, decoding, encoding, and sending messages by these NIO threads;

3) 1 NiO threads can process n links at the same time. However, 1 links only correspond to 1 NIO threads, preventing concurrent operation problems.

In most scenarios, reactor multithreaded models can meet performance requirements, but in very special applications. a NIO thread is responsible for listening to and handling all client connections and may have performance issues .

For example, the million-client concurrent connection, or the server needs to the client handshake message Security authentication, the authentication itself is very lossy performance. In such scenarios, a single acceptor thread may have a performance problem, in order to resolve performance issues. Produced a third reactor threading model-master-slave reactor multithreaded model.

That is, from a single thread to listen to a connection event, read and write events, read and write by the finished data, split into a thread dedicated to listen to various events. The dedicated thread pool is then responsible for processing the real IO data read and write.

Master-Slave reactor multithreading model

Master The biggest difference between the reactor threading model and the reactor multithreaded model is that there is a set of NIO threads that handle connection, read-write events.

The main feature of the master-slave reactor threading model is that the server is no longer a 1 separate NIO thread for receiving client connections. Instead, it is a separate NIO thread pool. Acceptor receives a CLIENTTCP connection request processing (possibly including access authentication, etc.), the newly created Socketchannel is registered on an IO thread of the IO-thread pool (sub reactor thread pool). It is responsible for the reading and decoding work of Socketchannel.

The acceptor thread pool is only used for client landing, handshake, and security authentication, and once the link is established, the link is registered on the IO thread of the backend Subreactor thread pool, which is responsible for the IO operation.

That is, from a multithreaded model, a thread listens for connection events and data read and write events, splits into a thread to listen for connection events, multiple threads of the thread pool listen to data read and write events for a connected socket, and, as with the multithreaded model, a dedicated thread pool handles real IO operations.


respective application Scenarios

NIO Application Scenario

The server needs to support an extremely large number of long-time connections . For example, 10,000 connections above, and each client does not frequently send too much data . For example, a central server in a head office needs to collect information about the transactions of various cash registers in a national convenience store. A large number of long-term connections that require only a small number of threads to handle maintenance.

Jetty, Mina, Netty, zookeeper, etc. are all based on the NIO method.

Bio Application Scenarios

Suitable for a smaller number of connections . And a scenario that sends a large amount of data at a time, in such a way that the server resource requirements are higher and are limited to the application.

Java Io:bio and NiO differences and their respective application scenarios

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.