views:

290

answers:

2

Do applet javascript calls serialize in any proper way? I'm considering implementing a model in which two threads send off javascript commands. One responds to requests from the browser (js), and the other hits the browser periodically. My question is this: what happens if these two javascript requests collide?

I'm worried about concurrent javascript requests, as intuition leads me to believe that one of the calls will fail nondeterministically.

A: 

Javascript is single-threaded. Only one block of code can be executing at any given time; any other event, method, etc. that fires will wait until the currently executing code terminates. Google Chrome's javascript engine may be an exception - I recall them mentioning some kind of multi-threaded javascript support in their features, but I'm unsure how it works. In IE 6, the browser UI will actually become unresponsive while JS code is running (if you stop at a breakpoint with keyword 'debugger', or have IE set to debug, for example).

RMorrisey
A: 

Serialize? Do you mean synchronize? JSObject works by passing primitives to a single JSContext JNI NPAPI wrapper which acts on the DOM in the underlying browser implementation. Are you referring to the passing of primitives through JNI?

I don't know much about how chrome's JS engine works, but I assume that since the liveconnect NPAPI in chrome is the same as the others, the ability to write multithreaded javascript has nothing to do with multithreaded JSObject access. The affected code extends the same abstract JSObject class that firefox uses.

There are several implementations of JSObject/liveconnect -- a few for netscape/firefox and one for ie (ocx). All behave differently as far as multiple threads go.

DOM access isn't threadsafe in general as far as liveconnect is concerned. So, officially, multiple threads accessing the DOM is undefined. If you manage to actually access the DOM through something other than code scoped in DOMService.run(), you'll get crazy deadlocks and race conditions.

So, basically, you need all of your java threads to post all DOMActions to your DOM access dispatch thread only.

Jon Bringhurst