How to achieve multithreading in Java Script

Achieve Multithreading in Java Script

Alright, before we start let me admit that JavaScript is a single-threaded environment, JavaScript historically suffers from an important limitation that all its execution process remains inside a single thread.

On the web you may found the various ways to archive multi-threading but as far as I know, the only way is HTML5 Web Workers. You can simulate ‘concurrency’ by using techniques like setTimeout(),setInterval() and event handlers.

Yes, all of these features run asynchronously, but non-blocking doesn’t necessarily mean concurrency. Like setInterval() only schedule things to happen later, they don’t run concurrently with anything else. Before multithreading when executing scripts in an HTML page, the page becomes unresponsive until the script is finished.

Multithreading using HTML5 Web Workers

Introducing Web Workers

Web Workers run in an isolated thread. A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. You can continue to do whatever you want: clicking, selecting, etc.,without blocking the UI or other scripts while the web worker runs in the background.There are two kinds of Web Workers Dedicated Workers and Shared Workers

I found this a very useful article to implement multi-threading using Web Workers.

Browser Support

Chrome: 4.0

Internet Explorer: 10.0

Mozilla : 3.5

Safari: 4.0

Leave a Reply

Your email address will not be published. Required fields are marked *