views:

3035

answers:

8

Is there some way to do multi-threading in JavaScript?

+1  A: 

In raw Javascript, the best that you can do is using the few asynchronous calls (xmlhttprequest), but that's not really threading and very limited. Google Gears adds a number of APIs to the browser, some of which can be used for threading support.

jsight
+6  A: 

There's no true threading in JavaScript. JavaScript being the malleable language that it is, does allow you to emulate some of it. Here's an example I came across the other day.

http://www.neilmix.com/2007/02/07/threading-in-javascript-17/

Svend
+1  A: 

JavaScript does not contain threads, at least in its current form. What exactly are you trying to do?

glaxaco
+21  A: 

The words you want to google for are JavaScript Worker Threads

Apart from from Gears there's nothing available right now, but there's plenty of talk about how to implement this so I guess watch this question as the answer will no doubt change in future.

Here's the relevant documentation for Gears: WorkerPool API

WHATWG has a Draft Recommendation for worker threads: Web Workers

And there's also Mozilla’s DOM Worker Threads


Update: June 2009, current state of browser support for JavaScript threads

Firefox 3.5 has web workers. Some demos of web workers, if you want to see them in action:

The Gears plugin can also be installed in Firefox.

Safari 4, and the WebKit nightlies have worker threads:

Chrome has Gears baked in, so it can do threads, although it requires a confirmation prompt from the user (and it uses a different API to web workers, although it will work in any browser with the Gears plugin installed):

  • Google Gears WorkerPool Demo (not a good example as it runs too fast to test in Chrome and Firefox, although IE runs it slow enough to see it blocking interaction)

IE8 can only do threads with the Gears plugin installed

Sam Hasler
which browsers support Web Workers? I know Firefox 3.5 does.. and it looks like that feature was pushed back and won't be in Chrome 2.
Jeff Atwood
Although Safari 4 supports web workers it appears that only Firefox supports passing complex objects via postMessage: http://hacks.mozilla.org/2009/07/working-smarter-not-harder/ See the last paragraph of that post on real world usage in the Bespin project for links to **a shim for that implements the Worker API in terms of Google Gears** and which **adds the missing features to the worker implementation of Safari 4** and details of how they implemented transparent custom events on top of the postMessage interface.
Sam Hasler
Now IE9 is out, you can update "IE8 can only do threads with the Gears plugin installed" to "IE8 and IE9 can only do threads with the Gears plugin installed"
BenoitParis
+3  A: 

There is no true multi-threading in Javascript, but you can get asynchronous behavior using setTimeout() and asynchronous AJAX requests.

What exactly are you trying to accomplish?

17 of 26
+2  A: 

You could use Narrative JavaScript, a compiler that will transforms your code into a state machine, effectively allowing you to emulate threading. It does so by adding a "yielding" operator (notated as '->') to the language that allows you to write asynchronous code in a single, linear code block.

Antoine Aubry
+1  A: 

The new v8 engine which should come out today supports it (i think)

Juan Manuel