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.
Community comments
The tutorial
by November Salazar,
Re: The tutorial
by Jon Gorrono,
Huge js file
by Sarath P,
Re: Huge js file
by Luciano Deriu,
Fascinating!
by Frederick Polgardy,
Error in code sample?
by Inderjit Gill,
script file
by dma dma,
Timer functionality in javascript.
by Andrei Sedoi,
Executing function on existing thread.
by Andrei Sedoi,
Is the function compilation recursive?
by Nick P,
The tutorial
by November Salazar,
Your message is awaiting moderation. Thank you for participating in the discussion.
is not available in English.
Re: The tutorial
by Jon Gorrono,
Your message is awaiting moderation. Thank you for participating in the discussion.
There is a link to a pdf in English:
jsthread.sourceforge.net/cgi-bin/wiki/wiki.cgi?...
Huge js file
by Sarath P,
Your message is awaiting moderation. Thank you for participating in the discussion.
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,
Your message is awaiting moderation. Thank you for participating in the discussion.
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...
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,
Your message is awaiting moderation. Thank you for participating in the discussion.
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,
Your message is awaiting moderation. Thank you for participating in the discussion.
The 4th code sample seems to have an incorrect exclamation character in the 'if' statement. Instead it should be:
script file
by dma dma,
Your message is awaiting moderation. Thank you for participating in the discussion.
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,
Your message is awaiting moderation. Thank you for participating in the discussion.
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,
Your message is awaiting moderation. Thank you for participating in the discussion.
Hi Daisuke,
Could you please show me how can I execute some function on existing thread?
Is the function compilation recursive?
by Nick P,
Your message is awaiting moderation. Thank you for participating in the discussion.
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?