Processes and threads
To understand synchronous blocking and asynchronous non-blocking in JS, we need to have a clear understanding of the basic concepts of processes and threads. Process: The basic unit for allocating and managing resources during the execution of concurrent programs. In simple terms, each program running on your computer is a process. Thread: A unit of execution in a process, the smallest unit in which an operating system can schedule operations. A program must have one process, and a process must have at least one thread. Let me give you an example
For example, your computer turned on thunder, thunder is a process; When you open QQ on your computer, QQ is a process. Every program you open on your computer creates another process. Take the QQ process for example, if you open the voice, then the voice is a thread in the QQ process, and if you open the video, it is the thread of the VIDEO in the QQ process.
Synchronous and asynchronous
We all know that JS is a single thread mechanism, it is divided into two working modes, synchronous mode and asynchronous mode. Synchronization: When a function call is made, the call does not return or continue until the result is received. Simply put, synchronization means that you have to do one thing at a time and wait until the previous one is done before you can do the next one. Asynchrony: As opposed to asynchrony, it is possible to proceed with a function call without receiving the result, and notify the caller when the call is complete. Generally speaking, it’s in the middle of being done, but you can move on to the next thing, and when it’s done, you’ll be notified.
Here’s a picture to illustrate it
Blocking and non-blocking
Blocking and non-blocking, usually referring to operations against IO. Blocking: In simple terms, we call a function and wait for the result of the function to return. If the current thread is suspended, it is called blocking. Non-blocking: As opposed to blocking, if the current thread is still running after executing a function, it is free to continue processing other tasks, but periodically checks to see if there is a result, it is non-blocking.
Synchronous blocking and asynchronous non-blocking
Blocking and non-blocking for the caller, synchronous asynchronous for the called, a combination of four states, namely
- A synchronized block
- Synchronous nonblocking
- Asynchronous blocking
- Asynchronous non-blocking Next, an example is used to illustrate the differences between the four, as shown in the figure below
Js is single threaded
A synchronized block
Asynchronous nonblocking