What is a process?

Process: is the smallest unit of resources allocated by the CPU; (The smallest unit that can have resources and operate independently)

What is a thread?

Thread: is the smallest unit of CPU scheduling; (A thread is a unit of running a program at a time based on a process. There can be multiple threads in a process.)

What is the difference between threads and processes?

A thread is a unit of execution that can execute program code during the execution of a program. Threads have four main states: running, ready, suspended, and finished. A process is a program that is executing. And thread is sometimes called lightweight processes, it is the smallest unit of program execution, a process can have multiple threads, sharing program memory space between each thread (code, data, and heap space) and some process level of resources (such as open files), but each thread has its own stack space,

Thread versus process metaphor

A process is a company, and each company has its own resources to schedule; Companies are independent of each other; A thread is each employee (you, me, him) in a company. Multiple employees work together to complete a task. The company can have one or more employees sharing the company space

The realization principle of multithreading

In fact, a single-core CPU can only execute one thread at a time, and multithreading is the CPU’s ability to switch (schedule) between multiple threads quickly, creating the illusion of multiple threads executing at the same time. If you have a multi-core CPU you can actually handle multiple threads at the same time.

JS single thread concept

  • Single threading refers to a sequence in which only one valid operation is performed, with a clear order of execution between the different operations
  • JS code is single threaded when it is executed by the JS engine. There is only one code executing in one place at a time, doing only one thing at a time, and blocking other code.

That’s an inconvenient name for a single thread, right? Why is it designed this way? There’s a reason for that.

JS as the script of the browser, therefore, its most fundamental role is to realize the interaction between the user and the browser. When A user wants to delete A and add content to A at the same time, if multithreading, how to deal with it? There is no way to handle this, if the single thread is very simple, the user performs that operation first, our main thread will do which operation, there is no conflict!

Why is a single js thread asynchronous

Asynchronous tasks are performed by other threads in the browser.

  • For example, if I have an asynchronous task buyFruit, how can I do something else after it has bought the fruit, say I need to eat the fruit I’ve bought, I can do this by passing in the function eat as an argument to the buyFruit function, so that when buFruit has a result, So eat can process the asynchronous result, and in this case, the eat function is called back.
  • What is a callback? Maybe you’ve seen concepts, been confused, and given examples of images. In fact, I think a lot of things don’t understand the concept, you know what it is, what it does, it doesn’t really work, if you have to ask what a callback is, first of all, a callback is a function, and it’s passed as an argument to another function. It’s that simple.
  • The problem with getting asynchronous results using callback functions, for example, is that it’s hard to handle when you have more asynchronous operations, which leads to other solutions, such as promises.

Js single thread and asynchronous parsing

Yes, JS is single-threaded, but JS is a scripting language that runs in the browser, and its host, the browser, is not single-threaded. At this point, you have to ask, what is the relationship between the two? Let’s talk about it:

A single thread cannot execute another task until the previous one has finished. If the queue is due to a large amount of computation and the CPU is too busy, it is fine, but many times the CPU is idle because the IO devices (input and output devices) are slow (such as Ajax operations reading data from the network) and have to wait for the results to come out before executing.

The designers of the JAVASCRIPT language realized that the main thread could simply ignore the IO device, suspend the pending task and run the next one first. Wait until the IO device returns the result, then go back and continue the pending task.
As a result, the task is divided into two types, one is synchronous task, one is asynchronous task
Synchronization task: The next task can be executed in the main thread only after the previous task is completed
Asynchronous tasks: This queue all task is not to enter the main thread of execution, but is provided by browsing the thread of execution, when after the completion of the execution will generate a callback function, and notify the main thread, in the main thread of execution of the current task, will obtain the earliest notification callback function, make it into the main thread of execution, such as ajax requests, The result of the request is rendered in the main thread

How does JS handle tasks

(1) All synchronization tasks are executed on the main thread, forming an execution context stack. (2) In addition to the main thread, there is a task queue. Whenever the asynchronous task has a run result, an event (callback) is placed in the “task queue”. (3) Once all synchronization tasks in the “execution stack” are completed, the system reads the “task queue” to see what events are in it. Those corresponding asynchronous tasks then end the wait state, enter the execution stack, and start executing. (4) The main thread repeats step 3 above.

Task queue

A “task queue” is a queue of events (also known as a message queue). When an IO device completes a task, it adds an event to the “task queue”, indicating that related asynchronous tasks can enter the “execution stack”. The main thread reads the “task queue”, which is to read what events are in it.

In addition to the IO device events, the task queue includes some user-generated events (such as mouse clicks, page scrolling, and so on). As long as the callback function is specified, these events are queued for the main thread to read when they occur.

The callback function

Callbacks are code that is suspended by the main thread. Asynchronous tasks must specify a callback function, which is executed when the main thread starts executing an asynchronous task.

A “task queue” is a first-in, first-out data structure in which the first events are read by the main thread. The reading of the main thread is basically automatic. As soon as the stack is cleared, the first event in the “task queue” automatically enters the main thread. However, due to the “timer” function mentioned later, the main thread first checks the execution time, and certain events cannot be returned to the main thread until the specified time.