Multi-Threading in JavaScript (HTML 5)
Introduction
Multi-threading is a basic technique in programming languages. Many applications and software services work using threads. Actually in OS level whatever runs are threads. Every programming language has its syntax and commands to use threading.
An interactive reach user interface should use multithreading. However JavaScript didn’t support multi-threading before HTML5. Now developers are able to use multi-threading features that support by new version of JavaScript within HTML5.
Web Worker
HTML 5 solution for implementing multi-threading is Web Worker. Web Workers let you run multiple processes in the background concurently. All workers run at the same time with together depends on the client computer speed. Using this threads you can make a network request, calculation or persisting data using local storage or fetchnig data from a database server.
Check if your browser supports Web Workers or not. Below function is undefined if it doesn’t.
function supports_web_workers() {
return !!window.Worker;
}
A Sample
Here you can find some samples of HTML 5. But I like to review another example together. Check this out.
I just want to highlight below parts. You can find the complete code here.
Here we have two separated thread. Ticker and Searcher:
// TICKER
var symbol = 'GOOG'; // default symbol to watch
var ticker = new Worker('ticker.js');
// SEARCHER
var searcher = new Worker('searcher.js');
function search(query) {
searcher.postMessage(query);
}
These thread work together at the same time. For JavaScript developers implementing of this kind of processes was just as a so far dream even in few years ago.
Posted in JavaScript
November 2nd, 2010 at 6:28 pm
refer to this blog
http://xebee.xebia.in/2010/11/02/multithreading-in-javascript-with-web-workers/