As a front-end girl, in order to make their own more steps, determined to improve their own technology. Let’s start with the basics
Threads and processes
concept
-
Process is a program with a certain independent function on a data set of a running activity, process is a system for resource allocation and scheduling an independent unit.
-
Threads A process may contain many sequential execution streams, each of which is a thread. A thread is an entity of a process. It is the basic unit of CPU scheduling and dispatch. It is a smaller unit that can run independently. A thread has virtually no system resources of its own, only a few resources that are essential to running (such as a program counter, a set of registers, and a stack), but it can share all of the resources owned by a process with other threads belonging to the same process.
An 🌰
- [
process
[: We all know programs. A program is static and usually stored in external storage. When a program is called into memory to run, it becomes a process. As the name implies, a process is an ongoing program, and it is a dynamic concept. It is the basic unit of system resource allocation and scheduling. For example, in Windows, every application or background program opened and running, such as QQ, Google Browser, netease Cloud Music, resource manager, etc., is oneprocess
. - [
Single thread
A process may contain many sequential execution streams, one for eachthread
.thread
And there was aSingle thread
withmultithreading
The points. Most of the programs we write in JS/C/Java are single-threaded. Use the main file as an entry to the program, and execute it in sequence until the entire code has been executed. - [
multithreading
Multithreading is when multiple sequential streams of execution execute “simultaneously” without interfering with each other. For example, if our web site background supports only single threads, then only one user can access the site at the same time, do you think this is realistic? The answer is yes. Therefore, we should create a thread for each user so that each user can access the site “simultaneously.” [At the same time
The technical term is concurrency.
By the way, let’s also look at concurrency versus parallelism, well, that’s not the point though… Concurrency: refers to, at the same time can only have one instruction (or a process, a thread) to run, but because the CPU rotation execution speed is beyond imagination, in the macro view, there are multiple instructions at the same time the effect of parallel: at the same time there are multiple instructions in multiple processors at the same time.
The difference between the two
The main difference between processes and threads is that they are different ways of managing operating system resources. Processes have independent address Spaces, a crash of one process in protected mode does not affect other processes, and threads are just different execution paths within a process. Thread has its own stack and local variables, but there is no separate address space between threads, a thread dead is equal to the whole process dead, so multi-process procedures than multithreaded procedures robust, but in the process switch, the cost of resources is larger, the efficiency is poor. However, for concurrent operations that require simultaneous and variable sharing, only threads, not processes, can be used.
- In short, a program has at least one process, and a process has at least one thread.
- The scale of thread partition is smaller than that of process, which makes the concurrency of multithreaded program high.
- In addition, the process has an independent memory unit during execution, while multiple threads share the memory, thus greatly improving the efficiency of the program.
- Threads are different from processes in their execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must depend on the application, which provides multiple thread execution control.
- From a logical point of view, the meaning of multithreading is that multiple parts of an application can be executed simultaneously. However, the operating system does not treat multiple threads as multiple independent applications to achieve process scheduling and management and resource allocation. This is an important difference between a process and a thread.
Blocking and non-blocking
concept
The concepts of blocking and non-blocking are related to the state of a program (thread) waiting for a message to be notified (whether synchronous or asynchronous). That is to say, blocking and non-blocking are mainly in terms of the state of the program (thread) waiting for a message notification.
-
A blocked call means that the current thread is suspended until the result of the call is returned, waiting for message notification and unable to perform other services. The function returns only after the result is obtained.
Note: for synchronous calls, many times the current thread may still be active, but logically the current function does not return, in which case the thread may process other messages as well
-
Non-blocking means that the function does not block the current thread, but returns immediately, until the result is not immediately available
While the non-blocking approach apparently improves CPU utilization, it also has the added consequence of increased thread switching on the system. Whether the increased CPU execution time will compensate for the system switching costs needs to be evaluated.
Synchronous and asynchronous
concept
synchronous
: Synchronization refers to a reliable task sequence in which the completion of one task depends on another task. The dependent task can only be completed after the dependent task is completed. Either success succeeds, failure fails, and the states of the two tasks remain the same.asynchronous
: Asynchrony does not need to wait for the dependent task to complete, but only notifies the dependent task of what work to complete, and the dependent task is executed immediately. As long as it completes the whole task, it is considered complete. As to whether the dependent task is actually completed in the end, the dependent task cannot be determined, so it is an unreliable task sequence.alerts
: Asynchrony is the opposite of synchronization. When a synchronous call is made, the caller waits for notification of a return message (result) before further execution. When an asynchronous procedure call is made, the caller does not immediately get the return message (result). The part that actually handles the call notifies the caller through status, notifications, and callbacks after completion.
There are three ways that the executor and caller return results: status, notification, and callback. Which notification mechanism is used depends on the implementation of the executing part and is not controlled by the caller unless the executing part provides multiple choices.
Blocking is non-blocking and has nothing to do with synchronous asynchrony. It can block and wait for synchronous or asynchronous execution to complete. Based on the above understanding, blocking and non-blocking/synchronous and asynchronous can be combined.
-
A synchronized block
The caller initiates an IO operation request, waits for the IO operation to complete and then returns. The I/O operation is performed after the operation is complete.
-
The synchronous non-blocking caller initiates an IO operation request, asks for the status of the IO operation, and returns immediately if it is not completed. If done, the result is returned. The RESULT of an I/O operation is returned only after the operation is complete.
-
An asynchronous blocking caller initiates an I/O operation and waits for the OPERATION to complete before returning. The IO operation process does not need to wait. After the operation is completed, the result is obtained through notification or callback.
The first thing to be clear about is that an asynchronous operation can be blocked, except that it is blocked not while processing a message, but while waiting for a message notification.
-
An asynchronous non-blocking caller initiates an IO operation request, asks for the status of the IO operation, and returns immediately if it is not completed. If done, the result is returned. The IO operation process does not need to wait. After the operation is completed, the result is obtained through notification or callback.
An 🌰
I love watching movies, so let’s take downloading movies for example
- Sync block: I hold my iPad and stare at its download bar until it’s 100% complete.
Synchronous embodiment: waiting for notification of download completion;
Blocking is the failure to perform other tasks while waiting for the download notification to complete.
- Sync is non-blocking: I download tasks and do laundry (yeah, I’m still a hard worker), then check the progress bar every once in a while, and when I see 100%, I’m done.
Synchronization is: waiting for notification of download completion
Non-blocking means waiting for a download notification to complete, doing something else, and just glancing at the progress bar every now and then.
- Asynchronous blocking. I switched to an app that notifies you when the download is complete. But I kept waiting for the ding. .
Asynchronous is reflected in: download complete “ding” a notice;
Blocking is reflected in: waiting for downloading to complete the “ding” notification process, can not do other tasks;
- Asynchronous non-blocking: Still the same download that dings. I submit the download and move on to something else. The “ding” tells me I’m done.
Asynchronous is reflected in: download complete “ding” a notice;
Non-blocking is reflected in: waiting for downloading to complete the “ding” notification process, to do other tasks, only need to receive the “ding” notification; [Software handles download tasks, I handle other tasks, do not need to pay attention to the progress, just receive software “ding” notification, can be]