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

Is it accurate to say JavaScript is a "single-thread" language? - Stack Overflow

programmeradmin2浏览0评论

I've heard this kind of statements many times, but personally I think this doesn't quite make sense. I think people are confusing JavaScript as a language specification and JavaScript in practice (browser, Node, etc.). Of course in most cases JavaScript are executed in a single-thread environment; but AFAIK nothing in the language specification requires it to be so. I think this is just like saying Python is "interpreted", while it's in fact entirely a matter of implementation.

So, is it accurate to say JavaScript is a "single-thread" language?

I've heard this kind of statements many times, but personally I think this doesn't quite make sense. I think people are confusing JavaScript as a language specification and JavaScript in practice (browser, Node, etc.). Of course in most cases JavaScript are executed in a single-thread environment; but AFAIK nothing in the language specification requires it to be so. I think this is just like saying Python is "interpreted", while it's in fact entirely a matter of implementation.

So, is it accurate to say JavaScript is a "single-thread" language?

Share Improve this question edited Jul 9, 2013 at 8:01 Derek Chiang asked Jul 9, 2013 at 7:46 Derek ChiangDerek Chiang 3,4106 gold badges29 silver badges37 bronze badges 1
  • This might be relevant stackoverflow./questions/30036/javascript-and-threads I guess there is nothing in the language itself that will disable you from creating multi-threaded interpreter – fsw Commented Jul 9, 2013 at 7:50
Add a ment  | 

3 Answers 3

Reset to default 7

By JavaScript you seem to mean ECMAScript.

There's already multithreading in the browser, built with webworkers, and based on a strong isolation of data : workers only municate by message passing, nothing is shared.

If you want more intricate multithreading, with data sharing, then that doesn't look possible now. Nothing in ECMAScript explicitely forbids multithreading but you can't do multithreading without

  • facilities to create "threads" (in a general sense, that could be coroutines)
  • mutexes and facilities to synchronize accesses
  • a low level support to ensure for example a property change won't break the data in case of simultaneous accesses. None of the current engines has been designed with this kind of strength (yes, some of them support multiple threads but in isolation).

The fact ECMAScript wasn't designed to include multi-threading is enough to prevent, currently, to support it (other than message-passing isolated multi-threading as is already done but it's a very limited kind of multi-threading).

You have to realize that

  • data sharing multi-threading is very expensive (not even speaking about simultaneous actions on the DOM)
  • you would rarely use it in JavaScript

Why do I say you would rarely use it ? Because most of the IO blocking tasks (file reading, requests, db queries, etc.), most of the low level tasks (for example image decoding or page rendering), most of the UI management (with event queue), most of the scheduling (timeouts and intervals) is done outside for you.

Multi-threading behavior is available in both HTML5 and node.js, BUT there is no native threading API in the Javascript language, so I guess the answer to your contrived question (I mean that in the nicest possible way, of course) is "yes, Javascript is a single-threaded language."

AFAIK nothing in the language specification requires it to be so.

In TC39 it says:

At any point in time, there is at most one execution context per agent that is actually executing code.

That appears to me to be the crucial guarantee that you never have to synchronize access to variables in ECMAScript. Which is what I expect is meant when someone says a language is single-threaded.

Of course, most ECMAScript host environments use more than one thread in their host environment implementation for things like garbage collection, etc. And, ECMAScript itself allows for multiple separate contexts of execution that could each be driven by their own thread--though the standard makes it clear you could also just drive all of them with the same thread.

The point, again, is you never have to protect any ECMAScript variable with a mutex, semaphore, or the like (which is why ECMAScript provides no such facilities) since the language promises there will never be two threads of control with simultaneous access to the same context.

I don't know of any JavaScript implementation that violates this promise either, though there certainly could be.

发布评论

评论列表(0)

  1. 暂无评论