Some questions about JavaScript's single-threaded and asynchronous

Source: Internet
Author: User

About JS single-threaded and asynchronous aspects of suddenly confused, see other people's article the more confused, feel this aspect is a pit, jump in is not good jump out. Again to see, look at the feeling that they understand some things, do not know right, anyway is to persuade himself, so understanding can understand the pass, summed up a few questions.

Issue 1: Browser Threads and processes

Read a lot of information, many people on the process and the thread is not differentiated, even above said that the process is changed to the thread. According to my understanding of the process is larger than the concept of a thread, when a program to open up a single or multiple process address space (IE, Firefox, Safari are single-process mode, chrome is a large number of processes), and then each process within the processing needs to open up their own threads for processing work, Each thread is inside the address space of each process, and each process has at least one thread, and all threads in the process share the process's address space.

1, process thread scheduling mode

2, [original]chrome browser JavaScript multithreaded-webworker (http://m.blog.csdn.net/blog/yl02520/14446763)

3. Chrome's process system (http://blog.csdn.net/xingtian713/article/details/4497798)

Problem 2:javascript Single Thread

The indisputable fact is that JavaScript is single-threaded and, for its special purpose, to avoid complexity, from the very beginning, JavaScript is a single thread, which has become a core feature of the language and will not change in the future. So be sure to clarify this point, but also to understand the concept of the single-threaded, understanding is not easy to be confused. JS single-threaded refers to a browser process only one JS of the execution thread, at the same time there will only be a piece of code in the execution (you can use IE tabbed browsing to try the effect, then open multiple pages using the same JS thread of execution, If one of the pages is performing a function with a large operand, the JS of the other windows will stop working).

The browser has many threads, is multi-threaded, when the JS thread executes, the browser can open the corresponding thread for processing according to the requirements.

In reference 2, the JS thread is really conflicting with the rendered thread, and the two can only execute one.

Reference 3: "In a browser, JavaScript execution and UI updates occur in the same process (browser UI thread). The work of the UI thread is based on a simple queue system, and the task is saved in the queue until the process is idle and executed. So JavaScript execution blocks UI updates, whereas UI updates block JavaScript execution. "What I understand is that the UI thread here is a process, and JavaScript execution and UI update are two threads that feel like they're getting dizzy by processes and threads.

1, JavaScript operation mechanism in detail: Another talk about event Loop (http://www.ruanyifeng.com/blog/2014/10/event-loop.html)

2. SetTimeout and SetInterval of JavaScript Asynchronous Programming (i) (HTTP://WWW.TUICOOL.COM/ARTICLES/EBUEUA)

3. JavaScript UI threading and performance optimization (http://zawa.iteye.com/blog/1270502)

Problem 3:HTML5 Webworker multithreading and JavaScript execution are single-threaded?

JavaScript executes is single-threaded, HTML5 's webworker allows JavaScript multithreading to divide the worker thread out of the original main thread, but the child threads are completely controlled by the main thread and must not manipulate the DOM. because the worker thread cannot directly access and manipulate the DOM property in the page, if the worker thread needs to access a DOM node in the page, it must send a message to the main thread via the PostMessage API, and the main thread gets the properties of a DOM node in the page after it receives the message , and then passed back to the worker thread in postmessage way, thus avoiding the conflict.

1, [Original]chrome browser JavaScript multithreaded-webworker (http://m.blog.csdn.net/blog/yl02520/14446763)

2, in-depth HTML5 Web Worker Application Practice: Multithreaded Programming (http://www.ibm.com/developerworks/cn/web/1112_sunch_webworker/)

Problem 4:javascript single-threaded, asynchronous, and Ajax?

The indisputable fact is that JavaScript is single-threaded, meaning that only one thread is currently running.

Ajax asynchronously loads the page, and the asynchronous loading of Ajax is like this:

It has a callback method, send the handle to the browser after the request, JS continues to execute other code, when the server returns the request, the browser "as soon as possible" call the previous callback method, is "as soon as possible", because there may be other currently executing methods should not be immediately interrupted.

Ajax and Multithreading okay

But Ajax can be implemented by multithreading, for the JS code there is only one thread, but does not mean that there is only one thread for the browser.

JS single-threaded refers to a browser process only one JS of the execution thread, at the same time there will only be a piece of code in the execution (you can use IE tabbed browsing to try the effect, then open multiple pages using the same JS thread of execution, If one of the pages is performing a function with a large operand, the JS of the other windows will stop working).

While the asynchronous mechanism is common to two or more resident threads of the browser, such as the asynchronous request is done by two resident threads: JS execution thread and the event trigger thread, the JS execution thread initiates an asynchronous request (then the browser opens a new HTTP request thread to execute the request, and the JS task is completed. Continue to perform other tasks remaining in the thread queue), and then at some point in the future the event triggers the thread to monitor until the previous HTTP request is completed, it will insert the completion event into the JS execution queue at the end of the JS processing. For example, timed triggering (settimeout and setinterval) is a timed count that is performed by the browser's timer thread, and then inserts the execution request of the timer processing function into the end of the JS execution queue at a timed time (so when using these two functions, The actual execution time is greater than or equal to the specified time, and does not guarantee accurate timing.

So, the so-called JS single-threaded and asynchronous more should belong to the browser behavior, there is no conflict between them, is not the same thing, no difference.

Async is relative , and the reference is a thread.

The concept is that the thread x does a task a without blocking the thread x so that threads X can do other tasks B. This has no conflict with language JavaScript that has a thread x point. Task A as long as the system, the operation of the bottom, the remote process to do it.

The language itself is single-threaded. At the same time, there is only one code that is executing. The same is true of the two functions that include settime.

All methods and functions that involve asynchrony are executed by another thread of the browser, and the "other thread" that the programmer can control is limited to these methods.

Ajax is entrusted to the browser execution, and the browser is multithreaded, will open a browser thread to submit request, not contradictory.

Because JavaScript is used in a browser, the browser itself is a typical GUI worker thread, and GUI worker threads are implemented as event handlers in most systems, avoiding blocking interactions and thus producing JavaScript-based asynchronous genes. Since then all this has come from.

The above is the reference 1 in the answer above, basically can understand JS single-threaded and Ajax asynchronous relationship.

The single thread of JS is related to its use. As a browser scripting language, JavaScript's primary purpose is to interact with the user and manipulate the DOM. This determines that it can only be single-threaded, otherwise it can lead to complex synchronization problems. So, the reality is that we need to let the browser work asynchronously, the browser will open up a thread to handle the AJAX request, the server returns the result will call the previous callback function, and the callback function is executed by the JS thread, the callback function will be added to the JS thread in the event queue to wait for execution.

1, JavaScript is both single-threaded and asynchronous, is there a conflict between the two, and what is the difference? (http://www.zhihu.com/question/20866267)

2. Async in JavaScript (http://cnn237111.blog.51cto.com/2359144/1556987)

Some questions about JavaScript's single-threaded and asynchronous

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.