BT

Article: Introducing Multithreaded Programming to JavaScript

by Ryan Slobojan on Jul 16, 2008 |

In this article, which was originally published on InfoQ Japan, Daisuke Maki describes some of the challenges of developing responsive AJAX applications, and presents Concurrent.Thread as a solution to easing the complexity involved in asynchronous communcation in AJAX.

From the article:

While increasingly more websites are fully or partially based on AJAX, it is still difficult to develop complicated AJAX applications. What is the main issue which causes this difficulty in developing AJAX applications? Is it asynchronous communication with the server, or is it GUI programming? Both are routinely performed by desktop window applications -- so why is development of AJAX applications which do the same things particularly difficult?
[...]
Let me talk about Concurrent.Thread, a library that allows JavaScript to use multiple threads, since this greatly eases the difficulty associated with asynchronous communication in the AJAX development mentioned above. This is a free-software library implemented in JavaScript, available under the Mozilla Public License / GNU General Public License. You can download the source code from the website.
[...]
With Concurrent.Thread, it is possible to switch execution context from one thread to another as needed even if you write a long and continuous program. Let me briefly talk about how this behavior is achieved. In short, code conversion is used. Very roughly speaking, the function passed to the create() method is first converted to a character string, which is then rewritten so that it can be executed on a piecemeal basis. Then, the rewritten function is executed little by little on the scheduler. The scheduler is responsible for coordinating multiple threads. In other words, it makes adjustments so that each of the rewritten functions will be evenly executed. Concurrent.Thread actually does not create new threads but simply simulates a multi-threaded environment on the original single thread.

Read the full article here.

Hello stranger!

You need to Register an InfoQ account or to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

The tutorial by November Salazar

is not available in English.

Re: The tutorial by Jon Gorrono

There is a link to a pdf in English:

jsthread.sourceforge.net/cgi-bin/wiki/wiki.cgi?...

Huge js file by Sarath Chandra P

The file itself is 499 kb - Which is large for javascript files. Even a YUI Compressed file is 289. The SVN has many modules under it, I am not sure if all modules are loaded in the *full* version. But If there is an on-demand *loader* module like YUI et all, it would be good.

Re: Huge js file by Luciano Deriu

Indeed the file is 500KB! which is huge. However that's not as bad as it sounds... if you Minify the JS file like you suggest you get it down to about 290KB. Now place that on your server and add compression and test it using this site...
www.gidnetwork.com/tools/gzip-test.php



On my simple Apache server with the mod_deflate enabled the actually KB transferred is only 46KB!


This roughly equates to having a Flash object on your page... I can see when making real web applications having multi threading JS will help a lot. I haven't yet tested Concurrent, largely because of its initial 500kb file size but now after testing out the "real world" impact i feel its not much of an issue(assuming it does what it says on the tin).

Fascinating! by Frederick Polgardy

This is a fascinating project. I'd be interested to see the code transformation happening in a pre-processing phase as well, if desired, to alleviate the browser from having to essentially implement a JavaScript compiler/code-emitter in JavaScript. Is it possible to generate transformed code with a standalone JavaScript interpreter?

Error in code sample? by Inderjit Gill

The 4th code sample seems to have an incorrect exclamation character in the 'if' statement. Instead it should be:

var cache = {};
function getArticleWithCache ( id, callback ) {
if ( cache[id] ) {
callback(cache[id]);
} else {
getArticle(id, function( a ){
cache[id] = a;
callback(a);
});
}
}

script file by dma dma

hey guys does anyone knows where i can get this script(Concurrent.Thread.js)?

the url does not work

thanks

Timer functionality in javascript. by Andrei Sedoi

I am currently dealing with the problem of measuring equal intervals of time using pure javascript. It will be great if this framework can address this issue.

Executing function on existing thread. by Andrei Sedoi

Hi Daisuke,

Could you please show me how can I execute some function on existing thread?

Is the function compilation recursive? by Nick P

Based on some testing, I noticed that if function A and function B are run in two separate threads (using Concurrent.Thread.create), then each function's body is broken up and executed piecemeal (with a chance to yield other threads after each statement) - thereby simulating true concurrency.

However, if function A calls function C, I don't think function C gets compiled. So if function C is long-running, then it has no chance to yield.

Is this assessment valid or is there a way to address this?

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

10 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2013 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT