Alternative JavaScript worker thread API

From Trephine

Jump to: navigation, search
« Implementing JavaScript worker threads Efficient JavaScript string building »

[subscribe] Recent blog entries

Live Demos

Alternative JavaScript worker thread API

Yesterday's article described how to implement JavaScript worker threads using trephine. That is, how to provide a convenient and simple API for spawning Java native threads written entirely in JavaScript. This article leads off with some more concrete examples of how to use that API, developing a framework for thinking about threaded web applications.

To quickly recap yesterday's article, recall that we had added some methods to the Function prototypes in both the browser and trephine/rhino contexts:

  • In both environments, we added a run() method which lets us use a function as the body of a new thread, and
  • In trephine/Rhino we added an eval() method which lets us execute code back in the browser DOM.

Note that the eval() method we defined is the logical converse of trephine.js(), which lets us run code in the trephine/Rhino context.

Here's a simple example to demonstrate the above API:

/**
 * Simple worker thread to multiply two values then updates the DOM.
 * @param Number x The first number.
 * @param Number y The second number.
 * @param string id The ID of the DOM element to update.
 */
var multiplier = function( x, y, id ) {
    var result = x * y;
    var updateElement = function( id, msg ) {
        document.getElementById( id ).innerHTML = msg;
    };
    updateElement.eval( id, result );
};

The multiplier function is intended to be used as the body of a worker thread, initialized through a call to multiplier.run(). For instance, if the page contained a <div> with an id value of "result", we could kick off a thread like this:

multiplier.run( Math.random(), Math.random(), "result" );

This would generate two random numbers (floating point values between 0 and 1) and pass them to multiplier.run() which spawns a Java native thread to do the work. The thread multiplies the results then calls its internal updateElement function to update the DOM.

Note that even though Java is invoked above, none of the code uses any Java classes or idioms directly. That is to say that the fact that Java is involved at all is an implementation detail. Another way to look at it is to consider what other implementations there may be. For example, Google's Native Client might have the capability to achieve the same.

Going the other direction, it's possible to get closer to the HTML 5 workers spec rather than introduce a new API like we have here. I'm currently working on a rudimentary implementation of parts of the spec in the interest of backporting worker functionality into older browsers. As soon as its ready, I'll post details here in the project blog.

Thanks for reading!

Public domain declaration

Just so there's no confusion: all of the code snippets on this page are provided "AS IS", without warranty of any kind, express or implied.

All of the code snippets on this page are hereby released into the public domain by the me, the copyright holder. This applies worldwide. Or in case this is not legally possible: The copyright holder grants any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.

If you'd feel better with a "real" license, you're free to use code snippets on this page under the MIT license as described on the about page.

Any links back to this site are always appreciated, but not required. Enjoy!

--Jim R. Wilson (jimbojw) 12:52, 28 April 2009 (UTC)
Personal tools