最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Are browsers still single-thread? - Stack Overflow

programmeradmin1浏览0评论

I listened to an old lecture from Crockford on Javascript (2006) and he was talking about the model

Flow  -> Paint
  ^        | 
  |        v
Script <- Event

(a loop involving Flow, paint, event, script ...), saying "because all browsers are single threaded".

So, my question is, are they still single threaded?

And for those who might know, are these video lectures still relevant in terms of what is taught?

I listened to an old lecture from Crockford on Javascript (2006) and he was talking about the model

Flow  -> Paint
  ^        | 
  |        v
Script <- Event

(a loop involving Flow, paint, event, script ...), saying "because all browsers are single threaded".

So, my question is, are they still single threaded?

And for those who might know, are these video lectures still relevant in terms of what is taught?

Share Improve this question edited Mar 5, 2015 at 1:46 Jongware 22.5k8 gold badges55 silver badges102 bronze badges asked Mar 5, 2015 at 1:40 SJPROSJPRO 3132 silver badges8 bronze badges 7
  • 5 Browsers aren't single threaded, but your script runs in a single runloop. – Ja͢ck Commented Mar 5, 2015 at 1:46
  • What @Ja͢ck said. Each tab gets its own thread. JavaScript runs an Event Loop that listens for callbacks to it saying, HEY I got something for you to do DOM. – pmac89 Commented Mar 5, 2015 at 1:49
  • 2 @pmac89: In fact even a single tab can have multiple threads, for everything that works concurrently. One for the DOM, one for reflows and repaints, one for the HTML parser, one for background events, and one for the script engine of the main browsing context. – Bergi Commented Mar 5, 2015 at 1:57
  • OH, because from a previous read I read the only one who ran tabs in their own threads is Chrome. Btw, I think some of you are referring to the inner workings of the browser, I was talking about something else .. more like the reply @Geohut gave. Sorry if I cant express myself quite right, english its not my native language, So I guess I confuse you !. – SJPRO Commented Mar 5, 2015 at 1:57
  • @Ja͢ck absolutely right. I just didn't want to add plexity to the idea of what the OP was trying to understand. Chrome is not the only one anymore, pretty much all modern browsers do this now to increase speed and to Sandbox each tab (and sometimes sandbox even more than just the tab). – pmac89 Commented Mar 5, 2015 at 2:03
 |  Show 2 more ments

3 Answers 3

Reset to default 3

Technically yes they are still all running single threads in the treatment of generating your page and actions on the front end. There are ways to make it seem like it isn't and run a javascript process independently like a pseudo multi thread using web workers introduced in html5. By pseudo I mean it works like most multi threaded processes it switches so fast between the clock interrupts it seems as if it's multi threaded. More information on web workers can be found at http://www.w3schools./html/html5_webworkers.asp as well as google of course.

In terms of the JavaScript you get to run, yes. This is normally not an issue because anything you intend to execute in JavaScript that might take more than a millisecond can be expressed as an "asynchronous" operation (for instance, AJAX requests, animation transitions, etc). That means all your code does is start the operation, and register possible callbacks for it to finish.

There are very infrequent exceptions to that where it makes sense to have a "Web Worker", but I have never actually seen those put to use.

Browsers are multi-threaded by Design.

In browsers, normal javascript code is executed by javascript runtime which is single-threaded. But Browsers like chrome use WEB APIS which has multiple threads to process blocking operations (such as setTimeout(), DOM, Ajax, etc) asynchronously using an event loop.

The browser might be seen as single-threaded because it runs normal javascript code in a single thread but other blocking operations are handled by multiple threads.

Remember normal javascript codes are processed in a single thread even if it is blocking like forEach(). If you have a forEach in your program the javascript won't be able to do anything unless the forEach() pletes but if you define setTimeout() then Web API will recognize it and pass it onto the hidden threads hence it will be processed asynchronously.

发布评论

评论列表(0)

  1. 暂无评论