Declare an empty array, open four processes and push values into the array at the same time. What is the order in which the pushes are entered?
高洛峰2017-06-12 09:25:56
I have never heard of the saying that js has multiple processes, and it is impossible to modify an array at the same time. You can post the code for other experts to take a look
欧阳克2017-06-12 09:25:56
A simple experiment is as follows. When multiple processes (tasks) operate, the value of the array depends on the last process. I use 2 setTimeout
and 1 setInterval
to simultaneously operate an array, and push
into three values 1, 2, and 3 respectively. The final result is:
[1, 2, 3, 3, 1, 1, 3, 1, 1, 3]
There is a clear explanation in Ruan Yifeng’s article: Single thread means that all tasks need to be queued, and the next task will not be executed until the previous task is completed. If the previous task takes a long time, the next task will have to wait. ;View original text
typecho2017-06-12 09:25:56
Just test it and you will know. Print this array every time the push is successful