1

It says in a lot of website that JavaScript is single-threaded. When they say this, do they mean the JavaScript runtime?

I might be misunderstanding something but isn't JavaScript just a programming language and the programs you create with it should be the ones labeled as single-threaded? But maybe I'm not understanding something so can someone please explain what I am don't getting?

g_b
  • 8,788
  • 7
  • 32
  • 64
  • possible duplicate http://stackoverflow.com/questions/21718774/how-is-javascript-single-threaded – Will.Harris Jan 18 '16 at 09:24
  • the language is single threaded would be more accurate I guess - if you want a half decent description of how js works, this [MDN page](https://developer.mozilla.org/en/docs/Web/JavaScript/EventLoop) does an OK job of it – Jaromanda X Jan 18 '16 at 09:25
  • 1
    @JaromandaX: The language isn't single-threaded. The language is near silent on the subject of threading, which is a very different thing. – T.J. Crowder Jan 18 '16 at 09:28

2 Answers2

7

JavaScript, the language, is nearly silent on the topic of threading. Whether it's single- or multi-threaded is a matter of the environment in which it runs. There are single-threaded JavaScript environments, and multi-threaded JavaScript environments. The only real requirement the specification makes is that a thread have a job queue and that once a job (a unit of code to run, like a call to an event handler) is started, it runs to completion on the thread before another job from that queue is started. That is, JavaScript has run-to-completion semantics.

JavaScript on browsers isn't single-threaded and hasn't been for years. There is one main thread (the thread the UI is handled on), and any number of web worker threads. The web workers don't have direct access to the UI, they send messaegs to the UI thread, and it does the UI updates. The threads don't directly share data, they share data explicitly via messaging. This separation makes programming multi-threaded code dramatically simpler and less error-prone than if multiple threads had access to the UI and the same common data area. (Writing correct multi-threaded code where any thread can access anything at any time is hard.)

Outside the browser, JavaScript in NodeJS is run on a single thread. There was a fork of Node to add multi-threading, but I don't think it ever went anywhere.

JavaScript on the JVM (Rhino, Nashorn) has always been multi-threaded, supported by the threading facilities of the JVM.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
2

While TJC's answer is of course correct, I don't think it addresses the issue of what people actually mean when they say that "JavaScript is single-threaded". What they're actually summarising (inaccurately) is that the run-time must behave as if has a single thread of execution, which cannot be pre-empted, and which must run to completion. The actual runtime can do anything it likes as long as the end result behaves in this way.

This means that while a JavaScript program may appear to be massively parallel with lot of threads interacting with each other, it's actually nothing of the sort. A kernel controls everything using the queue, event loop, and run-to-completion semantics (briefly) described here.

This is exactly the same problem that Hardware Description Languages (VHDL, Verilog, SystemC (though not actually a language), and so on) face. They give the illusion of massive parallelism by having a runtime kernel cycle between 'processes' which are not pre-emptible, and which must run until defined suspend points. The point of this is to ensure that models are executed in a determinate, repeatable fashion.

The difference between the HDLs and JS is that this is very well defined and fundamental for HDLs, while it's glossed over for JS. This is an extract from the SystemC LRM, which covers it briefly - it's much better defined in the VHDL LRM, for example.

Since process instances execute without interruption, only a single process instance can be running at any one time, and no other process instance can execute until the currently executing process instance has yielded control to the kernel. A process shall not pre-empt or interrupt the execution of another process. This is known as co-routine semantics or co-operative multitasking.

AndresM
  • 1,189
  • 8
  • 16
EML
  • 8,369
  • 6
  • 37
  • 72
  • I wouldn't call it "glossed over" for JS at all, not [as of the latest specification](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-jobs-and-job-queues). 'twas a bit vague (but well-established in some contexts) beforehand. – T.J. Crowder Jan 18 '16 at 11:44
  • Thanks, hadn't seen that. – EML Jan 18 '16 at 12:29