Home > Article > Web Front-end > Is javascript single threaded?
JavaScript is single-threaded. Even if JavaScript has multiple cores, it can only run tasks on a single thread called the main thread; as a browser scripting language, the main purpose of JavaScript is to interact with users and operate the DOM. If it is not single-threaded, it will bring a lot of trouble. Complex synchronization issues.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
As a browser scripting language, the main purpose of JavaScript is to interact with users and operate the DOM. This determines that it can only be single-threaded, otherwise it will cause very complex synchronization problems.
A thread is basically a single process that a program can use to complete tasks. Each thread can only execute one task at a time:
Task A --> Task B --> Task C
Each task will run sequentially; one task must complete before the next can begin.
As mentioned earlier, many computers now have multiple cores and therefore can perform multiple operations at once. Programming languages that can support multiple threads can use multiple cores to complete multiple tasks simultaneously:
Thread 1: Task A --> Task B Thread 2: Task C --> Task D
Traditionally, JavaScript is single-threaded. Even with multiple cores, you can only make it run tasks on a single thread called the main thread. Our example above runs like this:
Main thread: Render circles to canvas --> Display alert()
Over time, JavaScript gained some tools to solve such problems. Web workers allow you to send some JavaScript processing to a separate thread (called a worker) so that you can run multiple JavaScript blocks at the same time. Typically you will use a worker to run expensive processes on the main thread so that it does not block user interaction.
Main thread: Task A --> Task C Worker thread: Expensive task B
With this in mind, open your browser's JavaScript console again and take a look at simple-sync-worker.html (to see it running in real time). This is a rewrite of the previous example that calculated 10 million dates in a separate worker thread. Now when you click the button the browser will be able to display the paragraph before the date has finished calculating. The first operation no longer blocks the second.
A major feature of the JavaScript language is single-threading, which means that it can only do one thing at the same time. So why can't JavaScript have multiple threads? This can improve efficiency.
The single thread of JavaScript is related to its purpose. As a browser scripting language, JavaScript's main purpose is to interact with users and manipulate the DOM. This determines that it can only be single-threaded, otherwise it will cause very complex synchronization problems. For example, suppose JavaScript has two threads at the same time. One thread adds content to a certain DOM node, and the other thread deletes the node. In this case, which thread should the browser use?
So, in order to avoid complexity, JavaScript has been single-threaded since its birth. This has become the core feature of this language and will not change in the future.
In order to take advantage of the computing power of multi-core CPUs, HTML5 proposes the Web Worker standard, which allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and are not allowed to operate the DOM. Therefore, this new standard does not change the single-threaded nature of JavaScript.
Related recommendations: javascript learning tutorial
The above is the detailed content of Is javascript single threaded?. For more information, please follow other related articles on the PHP Chinese website!