Tuesday, October 25th, 2005

Multi-threaded JavaScript?

Category: JavaScript

<p>One of those weird quirks of our profession is that Java and JavaScript have so little in common. As Marc Andreessen recently said, “[Netscape] did JavaScript to try to be an intermediate bridge between HTML and Java.” And with the demise of LiveConnect and Applets in widespread use, it’s become pointless and needlessly confusing to continue to refer to ECMAScript as JavaScript. 99% of web developers have never and will never use JavaScript to script Java.

Except, the next major release of Java, slated for 2006, will include a Sun-modified version of the Java-based Rhino JavaScript engine as an integrated part of the platform. Interesting to see JavaScript back in its role as a script wrapper around Java.

And now to the point of this blog entry. Depending on who you talk to, the lack of threading features in JavaScript is one of its best or worst features. Using a pre-release of Java 6, Sun engineer A. Sundararajan uses the new embedded JavaScript to go beyond simply using an external library to create a thread:

How do we write multithreaded scripts? JavaScript engine allows Java method calls. So, we can use that to create script wrappers for Java platform classes… we can easily create threads in script. But, what about mutual exclusion/communication/co-operation between threads? You may want locks, condition variables etc. Well, you may write few script wrappers using java.util.concurrent API. as shown below:

// creates a ReentrantLock
function lock() {
return new java.util.concurrent.locks.ReentrantLock();
}

// runs given function after acquiring given lock and releases the lock
// after the function is finished. (in effect the script function is
// ‘synchronized’ in Java sense
Function.prototype.sync = function(lock) {
lock.lock();
try {
this.call();
} finally {
lock.unlock();
}
}

The caller of the above code would look like:

function myFunc() {
// code here
}

// create a lock
var lk = lock();

// run myFunc in ‘synchronized’ way
// no two threads can run myFunc concurrently.
myFunc.sync(lk);

The latest released version of Java includes a state-of-the-art concurrency library; using it from JavaScript will be the source of endless amusement.

Of course, this has no application whatsoever for Ajax in the short-term, but it very well may provide a playground for developers to figure out what threading features they may want (if any) in future browser-based JavaScript engines.

Related Content:

Posted by Ben Galbraith at 9:54 am
10 Comments

+++--
3.4 rating from 38 votes

10 Comments »

Comments feed

You can already control multi-threading with a good AJAX engine. At Backbase we support locking of Event Handlers, and also sequential/parallel control within an Event Handler.

This is a less visible feature of AJAX engines, but a very useful one. The first thing most people see are the widgets (and maybe the XHR object) but that’s just the tip of the iceberg when developing AJAX-based rich internet applications.

Comment by Jep Castelein — October 25, 2005

Um, unless I missed something, XHR doesn’t do multithreading. The events come unpredictably but all on one thread.

Comment by Tom — October 25, 2005

For clarity, my comments were in response to Jep’s.

Comment by Tom — October 25, 2005

FWIW, Rhino has had Threads for some time. Either Sun just did (more?) needless engineering work or are trying to give JS code access to Java thread contexts which might be useful, although I’d say the jury is out on that.

For reference, have a look at “spawn and sync” at http://www.mozilla.org/rhino/shell.html

Regards

Comment by Alex Russell — October 25, 2005

To Tom: a toolkit can provide an additional layer to manage threading: so you don’t use XHR directly anymore, but you use an abstraction. This saves you time, and it gives you more control over the execution flow. This does not only apply to XHR, but to all client-side actions such as animations, calculations and transformations.

Comment by Jep Castelein — October 25, 2005

I have a raw example which would not have been that fast if I did not implemented multithreading techniques. It needs some polishing but the idea is to change views on a single dataset without postbacks to the server.

http://www.mschopman.demon.nl/test.htm

Comment by M. Schopman — October 26, 2005

Can anyone post a URL here to something describing how to implement a mutual exclusion mechanism in raw Javascript (IE friendly version)
such that one could use it for AJAX (i.e. so that background data structure manipulation in response to an XMLHTTP reply doesnt confuse foreground UI processing).

My project is developing a library/framework from scratch and cant rely on something like Backbase mentioned above, and, alas, in the deployment environment we cant rely on Java being available in the browser.

Comment by Bruce — December 28, 2005

Can anyone post a URL here to something describing how to implement a mutual exclusion mechanism in raw Javascript (IE friendly version)
such that one could use it for AJAX (i.e. so that background data structure manipulation in response to an XMLHTTP reply doesnt confuse foreground UI processing).

My project is developing a library/framework from scratch and cant rely on something like Backbase mentioned above, and, alas, in the deployment environment we cant rely on Java being available in the browser.

Comment by Bruce — December 28, 2005

I never got an answer to my question above, so I developed a method and wrote an article about it. See “AJAX from Scratch: Implementing Mutual Exclusion in JavaScript” on developer.com (Gamelan) at http://nl.internet.com/ct.html?rtr=on&s=1,2awq,1,2p40,99nr,is13,9j9z

Comment by Bruce — March 25, 2006

Correct me if I’m wrong guys, but HOW can multi-threading even be possible with JavaScript?

Lets say you’re running asynchronous requests on one window. There are lots of ways of managing it so that no two requests get run at the same time.

BUT NOW.. you open a new window. All the JavaScript code in the new window is separate. There is no way for one window to control the JavaScript execution of the other window. Yet, having requests go at the same time on both windows WILL freeze Firefox.

I run AJAX requests on my site. IE has no problem handling them, since it supports multi-threading, but Firefox always freezes. Its very annoying and I’d love to find a solution for this problem.

Comment by arminebusch — February 16, 2009

Leave a comment

You must be logged in to post a comment.