Java I/O model

Source: Internet
Author: User

This article forwards from the technical World , the original link http://www.jasongj.com/java/nio_reactor/

Synchronous vs. asynchronous

Synchronous I/O 

Each request must be processed individually, and the processing of a request causes a temporary wait for the entire process,

These events cannot be executed concurrently. After the user thread initiates an I/O request, it waits or polls the kernel I/O operation to complete before proceeding.

Synchronization means that one of the processes of a task is serialized with multiple threads

asynchronous I/O 

Multiple requests can be executed concurrently, and the execution of a request or task does not result in a temporary wait for the entire process.

The user thread continues execution after initiating an I/O request, notifies the user thread when the kernel I/O operation is complete, or invokes a callback function registered by the user thread.

Async means that a process can allow multiple threads to process at the same time.

Blocking vs. Non-blocking

Blocking 

After a request is issued, the request operation is blocked and will not return until the condition is satisfied because the requested operation requires a condition that is not satisfied.

Non-blocking 

After the request is issued, if the required conditions for the request are not met, a flag message is returned immediately informing that the condition is not satisfied and not waiting.

It is generally necessary to retrieve the request result by looping to determine whether the request condition is satisfied .

It is important to note that blocking is not equivalent to synchronization, and not blocking is not equivalent to Asynchrony.

In fact, these two sets of concepts describe the two different dimensions in the I/O model.

Synchronous and asynchronous focus is on the execution of multiple tasks, whether the post-launch task must wait until the first-initiated task is completed.

Regardless of whether the first-initiated task request is blocked waiting for completion, or if the request is immediately returned through a loop to succeed.

blocking and non-blocking focuses on whether the requested method returns immediately (or whether it is blocked when the condition is not met).

Four I/O models in Java

The first blocking I/O provided by Java is blocking I/O,

As described above, blocking I/O requests cannot be completed immediately, then remain blocked.


Blocking i/O is divided into the following two stages:
Phase 1: Wait for data to be ready. the case of network I/O is waiting for the remote data to arrive,and the disk i/O is waiting for the disk data to be read from disk to the kernel state memory.
Phase 2: Data copy. For system security, the user-state program does not have permission to read the kernel memory directly, so the kernel is responsible for copying the data in the kernel state memory into the user-state memory.

And NiO is non-blocking I/O,

Non-blocking i/O requests contain the following three stages 1) socket set to Nonblock (non-blocking) is to tell the kernel,

When the requested I/O operation cannot be completed, do not sleep on the thread, but return an error code (EWOULDBLOCK) so that the request does not block.
2) I/O operation function will continuously test whether the data is ready, if not ready, continue the test until the data is ready.

Throughout the I/O request, the user threadcan return immediately after each i/O request, but in order to wait for the data, it still needs to poll and repeat the request, consuming a lot of CPU resources.
3) The data is ready to be copied from the kernel to the user space.

This model is rarely used directly, but isa feature of non-blocking I/O in other I/O models. This approach has little meaning for individual I/O requests, but provides the conditions for I/O multiplexing.

At the same time, the reactor mode realized by NIO is the implementation of I/O multiplexing model .

I/O multiplexing (asynchronous blocking i/O)
I/O multiplexing uses the Select or poll function, which also causes the thread to block,

However, unlike blocking I/O, these two functions can block multiple I/O operations at the same time.

You can also detect multiple read operations, I/O functions for multiple writes, and then actually invoke the i/O operation function until there is data readable or writable. From a process point of view,there is not much difference between the I/O request and the synchronous blocking model using the Select function, and even more additional work is added by adding the monitor channel and calling the Select function for additional operations.

However, the biggest advantage after using select is that the user can process multiple channel I/O requests simultaneously in a single line range.

The user can register multiple channel, and then constantly call Select to read the activated channel, to achieve the same line range simultaneously processing multiple i/O requests.

In the synchronous blocking model, it is necessary to achieve this goal through multithreading. Call Select

By performing non-blocking I/O polling with a full-time user-state thread, the simulation implements phase-one asynchrony.

The proactor pattern implemented through AIO is the implementation of the asynchronous I/O model .

Call the Aio_read function, tell the kernel description word, buffer pointer, buffer size, file offset, and how to notify, and then immediately return.

Notifies the application when the kernel copies the data to the buffer. Therefore, in asynchronous I/O mode, Phase 1 and Phase 2 are all completed by the kernel and do not require the participation of the user thread.

Java I/O model

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.