JavaIO model

BIO model (BKICJ-LO Synchronous blocking IO)

If one thread processes a request, where does the other thread block

Where do accept() and read() methods block in IO stream operations

Synchronization and blocking, the server implementation mode is one connection one thread, that is, the client has a connection request server needs to start a thread for processing,

If this connection does nothing it causes unnecessary thread overhead.

Multiple threads need to be started, and thread switching and waiting can be very expensive

Application scenario: A fixed architecture with a small number of clients requires high server resources. The only choice before JDK1.4, simple and easy to understand.

NIO model (synchronous non-blocking)

Synchronous non-blocking (starting with JDK1.4), the server implementation mode is to process multiple requests from a single thread, since all connection requests sent by the client are registered with the multiplexer, which polls the connection to have IO requests processed.

One thread handles multiple read/write requests.

There are three major and parts: channel, buffer, selector

Buffer: an area of memory with an array at the bottom. Essentially a block of memory from which data can be written and then read is wrapped as a NIO Buffer object.

It also provides a set of methods to easily access the block memory, making the Buff API easier to manipulate and manage than operating directly on arrays

Channels: Java NIO is like streams, but different: you can read data from a channel and write data to a channel, but streams can only write or read.

Channels can be non-blocking read and write channels, and channels can support reading or writing buffers, as well as asynchronous reads and writes.

Selector: The selector is responsible for checking one or more channels. It is responsible for the reincarnations channel to check whether there is data in it. Select to process.

Each channel corresponds to a buffer, one thread to a selector, and one selector to multiple channels

Which channel the program switches to is determined by the event, and the selector switches on each channel according to the event.

Causes a thread to send requests or read data from a channel, but it can only get the data currently available, if no data is currently available.

Nothing is fetched, rather than keeping the thread blocked, so it can continue doing other things until the data becomes readable.

Let accept() and read() not block

The problem of implementing single thread concurrency

Because threads are waiting for resources, waiting for clients to link, waiting for clients to enter data

Application scenario: Suitable for chat server architectures with a large number of connections and a short connection time

AIO model (asynchronous non-blocking)

When reading or writing, you simply call the API’s read or write methods directly. Both methods are asynchronous

NIO2.0 asynchronous non-blocking, server implementation mode is one valid request one thread. Client I/O requests are completed by the OS and then notified the server application to start the thread for processing.

Application scenario: Server architecture with a large number of connections and a long connection time (supported only after 1.7)