I. What is synchronization? What is async?
The concept of synchronous and asynchronous has been coming out for a long time, and there are many other online statements about synchronization and asynchrony. Here is my personal understanding:
Synchronization is: If there are multiple tasks or events to occur, these tasks or events must be carried out individually, an event or the execution of a task will cause the entire process of temporary wait , these events can not be implemented in parallel;
Asynchronous is: If there are more than one task or event, these events can be executed concurrently, an event or the execution of a task will not cause the whole process of temporary waiting .
This is synchronous and asynchronous. For a simple example, if there is a task that includes two subtasks A and B, for synchronization, when a is in the process of execution, B waits until a executes, B can execute, and for async A and B are executed concurrently, B does not have to wait for a to execute, This will not result in a temporary wait for the entire task due to the execution of a.
In fact, synchronization and Asynchrony are a very broad concept, and their focus is on whether the occurrence or execution of an event causes a temporary wait for the entire process when multiple tasks and events occur. I think it's possible to associate synchronous and asynchronous with the Synchronized keyword in java. When multiple threads access a variable at the same time, each thread accesses the variable as an event, and for synchronization, it is the thread that must access the variable individually, while one thread accesses the variable, while the other threads must wait, and for async, multiple threads do not have to access the variable individually. can be accessed at the same time.
As a result, individuals feel that synchronization and asynchrony can manifest themselves in many ways, but it is important to remember that when multiple tasks and events occur, the occurrence or execution of an event results in a temporary wait for the entire process. In general, asynchronous can be implemented in a multi-threaded way, but remember not to put the multi-threaded and asynchronous equal sign, asynchronous is only a macro mode, the use of multithreading to achieve asynchronous is only a means , and through a multi-process way can also be implemented asynchronously
Two. What is blocking? What is non-blocking?
Blocking is: When an event or task is executed, it makes a request operation, but because the request operation needs the condition is not satisfied, then will wait until the condition is satisfied ;
Non-blocking is: When an event or task is executing, it issues a request action, and if the requested action requires a condition that is not met, it returns a flag message that the condition is not met and will not wait there .
This is the difference between blocking and non-blocking. That is, the difference between blocking and non-blocking is that when a request is made, if the condition is not met, it waits or returns a flag message.
To give a simple example:
If I want to read the contents of a file, if there is no content in the file readable, for the synchronization will be waiting until the contents of the file is readable, and for non-blocking, will directly return a flag information to inform the file is temporarily no content readable.
There are some friends on the web that equate synchronous and asynchronous with blocking and non-blocking, in fact, they are two completely different sets of concepts. Note that understanding the differences between these two sets of concepts is important for understanding the later IO model.
Synchronous and asynchronous focus on the execution of multiple tasks, whether the execution of a task will lead to a temporary wait for the whole process ;
While blocking and non-blocking focuses on issuing a request operation, if the condition of the operation does not meet whether it will return a flag message that the condition is not satisfied .
Understanding blocking and non-blocking can be interpreted in the same way as thread blocking, when a thread makes a request operation and if the condition is not met, it is blocked, that is, waiting for the condition to be satisfied.
Three. What is blocking IO? What is non-blocking IO?
Before understanding blocking IO and non-blocking IO, let's look at how the next specific IO operation is done.
Typically, IO operations include reading and writing to the hard disk, reading and writing the socket, and reading and writing the peripherals.
When the user thread initiates an IO request operation (This article takes the read request operation as an example), the kernel checks to see if the data to be read is ready, and for blocking IO, if the data is not ready, it waits until the data is ready and, for non-blocking IO, if the data is not ready, A flag message is returned informing the user that the data currently being read is not ready. When the data is ready, the data is copied to the user thread, which completes a full IO read request operation, which means that a complete IO read request operation consists of two phases:
1) See if the data is ready;
2) Copy the data (the kernel copies the data to the user thread).
The difference between blocking (blocking IO) and non-blocking (non-blocking io) is that the first stage , if the data is not ready, is waiting in the process of viewing the data is ready, or directly returning a flag message.
In Java, the traditional io is blocking IO, such as through the socket to read the data, after calling the read () method, if the data is not ready, the current thread will be blocked in the Read method call there until there is data to return, and if the non-blocking IO, when the data is not ready, read ( ) method should return a flag message informing the current thread that the data is not ready, rather than waiting there all the time.
Four. What is sync io? What is asynchronous IO?
Let's take a look at the definition of synchronous IO and asynchronous IO, as defined in the book UNIX Network programming for synchronous IO and asynchronous IO:
A synchronous I/O operation causes the requesting process to being blocked until that I/O operation completes.
An asynchronous I/O operation does not cause the requesting process to be blocked.
It can be seen from the literal meaning: synchronous io is a thread that, if requested by an IO operation, is blocked until the IO operation is completed;
Asynchronous IO does not cause the request thread to block if the IO operation is requested by one thread.
In fact, the synchronous IO and asynchronous IO models are for the interaction of the user thread and the kernel :
For synchronous IO: After the user makes an IO request operation, if the data is not ready, the user thread or the kernel is required to continually poll the data for readiness, and when the data is ready, the data is copied from the kernel to the user thread;
Asynchronous IO: Only the issue of the IO request operation is performed by the user thread, and the two phases of the IO operation are automatically completed by the kernel and then sent a notification informing the user that the thread IO operation has been completed. That is, in asynchronous Io, no blocking is generated for the user thread.
This is the key difference between synchronous IO and asynchronous Io, where the key difference between synchronous IO and asynchronous IO is reflected in whether the data copy phase is done by the user thread or the kernel . Therefore, asynchronous IO must have the underlying support of the operating system.
Note that synchronous IO and asynchronous IO are two distinct sets of concepts that are different from blocking IO and non-blocking IO.
Blocking IO and nonblocking io are reflected when the user requests an IO operation, if the data is not ready, whether the user thread is waiting for data to be ready, or receives a flag message above it. That is, blocking IO and non-blocking IO are reflected in the first phase of the IO operation, and how the data is handled when it is ready to be viewed.
Java NIO (GO)