16

As far as I understand, all JavaScript code is event-driven and executes on a single browser thread.

However, I have some JavaScript functions that are called from within a SWF object sitting on the same page. Is this code run in the same manner as regular JS code, or is it on some separate Flash thread?

If it is on a separate thread, can I use setTimeout() to get it to run on the JS events thread? e.g.:

function calledFromFlash() {
    setTimeout(doActualWork, 0);
}

function doActualWork() {
    // blah blah blah
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Karthik
  • 403
  • 3
  • 14

2 Answers2

10

It's still on the same thread. However, for most practical purposes if you have such a long-running JavaScript that you're worried your "main" task might block the call from setTimeout, you should consider revisiting your underlying approach.

Update for the bounty:

To expand on the more general question of threading in JavaScript, there is a great discussion with a very revealing answer from Bobince. He cites some very interesting scenarios that might call into question whether we can truly consider JS to be single-threaded, and his conclusion is "not quite".

The conclusion of the comments, which I agree with, is that from the perspective inside the JS runtime, the universe is single-threaded, but because the infrastructure surrounding the JS sandbox is not single-threaded, it can reach inside the sandbox and muck with state in unexpected ways. From inside the runtime, some unknown entity can "suspend the laws of nature" and change things around. But the runtime has no threading construct to handle that scenario natively.

I think the most important way to approach the question is to ask what do we mean by multi-threadedness in a practical scenario? Usually threading issues come down to things like synchronization, which we have to assume the browser vendors have solved for us because again, JavaScript has no native construct for even trying to deal with it ourselves. Hand-wringing about threading does no good without the tools to fix it; no mutexes or locks.

So setting aside those kinds of catastrophic problems, we're down to things like maybe a value gets changed out from under us unexpectedly. But well-written code should be OK with that. Even in Bobince' example, all the code involved is still code that we voluntarily included in the page (even wrote ourselves) so sure, it might be surprising if that code gets fired while your main callstack is ostensibly "blocked". But again speaking to practical problems, what is the worst thing you could do to yourself in that scenario? Nothing too serious.

So that's my long way of saying: I don't know of any documentation from the browser vendors where they say unequivocally whether their JS implementation is single-threaded or not, but I question whether that matters.

Community
  • 1
  • 1
Rex M
  • 142,167
  • 33
  • 283
  • 313
  • Hi Rex, Thanks a lot for your answer. A follow-up: is this true cross-browser, and do you know any references that support it? I'm not worried about the call being blocked, I just want to make sure that there's no synchronization issues between the callback code and my regular events. – Karthik Feb 06 '09 at 00:46
  • JavaScript simply has no mechanism for coping with multiple threads. This in itself is evidence it is *not* multi-threaded. This post is not evidence per se but raises the excellent point that concerns of threading in JS are pointless: http://damienkatz.net/2006/04/how_to_create_a.html – Rex M Feb 06 '09 at 01:38
  • If you search Google for info on threading in JS, you will find a lot of blogs that say JS is threaded, but it is all based on a misunderstanding of how the JS engine works in relation to its host (the browser). – Rex M Feb 06 '09 at 01:40
  • @Rex: Hi Rex, I started a bounty on this question in the hope of some more evidence; I understand that JS in the browser is single threaded, and that that's pretty darn good evidence in itself, but it's not really conclusive *proof*. If you could dig up something more substantial (maybe in the Firefox plugin API source?), or maybe cook up a small script that somehow shows the JS side is never concurrent, I'd be grateful. Don't get me wrong, your answer is already sufficient; I only started the bounty out of curiosity over whether any irrefutable proof could be dug up, ideally for each browser. – Cameron Jan 22 '12 at 05:35
  • @Cameron there is really good discussion on this topic over at another question. Bobince' answer is thorough but most of the commenters (and I myself) disagree with his conclusion that JS isn't *entirely* single-threaded. From the perspective of inside the JS sandbox, the universe is single-threaded. But to Bobince' point, the infrastructure *surrounding* the sandbox is multithreaded and can sometimes reach inside and muck with the call stack in unexpected ways. (Discussion: http://stackoverflow.com/questions/2734025/is-javascript-guaranteed-to-be-single-threaded) – Rex M Jan 22 '12 at 22:31
  • @Cameron I moved this into the answer and expanded on it a bit. – Rex M Jan 22 '12 at 22:51
2

Flash ExternalInterface calls are done synchronously using the same processing thread as your main application. Calls from Flash to JS are treated the same as any event binding in your JS application.

I've blogged about using this to your advantage when necessary, though it is more often a hassle.

Here's some other resources referring to this fact: link link link link

I hope that helps clarify things.

James Tomasino
  • 3,520
  • 1
  • 20
  • 38
  • Some links are broken, I'm having an issue that might be related, could you fix the links? – HLL Jun 15 '15 at 15:38
  • I'm not sure about any of the other links that may have expired, but my blog link changed to this: https://labs.tomasino.org/as3-synchronous-url-xml-loading.html – James Tomasino Jun 26 '15 at 15:56