Multi-Threading in browsers


What a sign of relief for Javascript programmers when you say something like that “Threading in Javascript”. Brendan Eich the creator of javascript is totally against the implementation of Multi-Threaded architecture in Javascipt due to the problems it has like deadlock, race-conditions…etc and he strongly believes that even with smater programming these cannot be totally eradicated. But with the advent of Gears API by Google there was a way-out on multi-threading in Javascript. But to get things done it needed the Google Gears plugin-in to be installed on the browsers. With recent HTML5 advancements towards a better web, web-workers are here for rescue. Web Workers work on the similar concept as that of Gears API [but the coding style differs a lot]. To avoid all those problems of multi-threading a message passing way is implemented in web-workers. Also the web-worker is not allowed to access the parent page DOM. Following is a basic simple example of Web Workers.

var worker = new Worker(js/myworker.js);
worker.onmessage = function(e){
        console.log(“Processed Message from Web-Worker : ” + e.data);
}
var somejsontoprocess = ‘{“name” : “Dominic”, “lastname” : “Dsouza”}’;
worker.postMessage(somejsontoprocess);
 
====contents of js/myworker.js=====
onmessage = function(e){
        var jsonfrommainthread = e.data;
        console.log(“Message from Main Thread : ” + jsonfrommainthread);
        //SOME LENGTHY PROCESS WHICH TAKES >10sec TO PROCESS
        postMessage(jsonfrommainthread);
}
==========================

Following is the pictorial representation of the above code:

webworkers

As shown in the above example, Parent thread passes a message to web-worker and the web-worker in turn does some lengthy process and replies with a processed message to the parent thread. So Due to web-workers the lengthy process is run in a different thread handled by the OS and the Main/Parent thread is left free and still active accepting inputs from user with no freezing of the browser.

In the above example if we remove web-workers and do a standard javascript programming with no setTimeouts, the browser window would freeze for the time the lengthy process is underway or may give an error to stop the lengthy script.

So to conclude: Web Workers will change the way we think about threading in Javascript and would reduce user waiting times and would make the browser more responsive.

FURTHER READING:

1. Even Faster Web Sites by Steve Souders [http://stevesouders.com/efws/] — second chaper explains everything gears, web workers.

2. Web Workers By Ido Green [http://shop.oreilly.com/product/0636920024446.do] — explains in-depth about Web Workers, types of web workers, techniques of message passing… etc. nice book!.

Advertisement

About Dominic

J for JAVA more about me : http://about.me/dominicdsouza
This entry was posted in Thechy Stuff and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s