HTML and CSS Reference
In-Depth Information
chapter 10
n n n
Multithreading in Web Pages
Using Web Workers
The creators of JavaScript invented the language as an aid to developing interactive web pages. HTML
markup by itself is purely static in nature and lacks any programming abilities. JavaScript was introduced
to compensate for this lack of programmability. Considering the needs that were felt that time, JavaScript
was created as a simple, lightweight, easy-to-use language.
In earlier days, JavaScript was primarily used to add interactivity and fancy graphical effects to
otherwise static web pages. However, over the years the situation has changed dramatically. Modern web
applications rely extensively on JavaScript for a variety of tasks. A web page of a modern web application
no longer uses JavaScript merely for fancy hover effects or animations. It relies heavily on JavaScript for
business validations, making Ajax calls to the server, and business domain-specific processing.
A web page that uses JavaScript executes the script in foreground. That means as long as the script is
running, user interaction with the page is blocked. In other words, user interactions and the script run on
a single thread. Such an approach can be troublesome when the script is performing an intensive and
time-consuming operation. To overcome this limitation, HTML5 and JavaScript provide web workers that
allow you to run JavaScript processing in the background. This chapter gives you a detailed understanding
of web workers. Specifically, you learn the following:
• What web workers are and how they work
• Flavors of web workers
• Restrictions on web workers
• Using web workers to develop a multithreaded web page
• Communicating with the server from the worker thread
Overview of Multithreading in Web Pages
If you've ever developed desktop applications in C# or Visual Basic, chances are you're aware of the Thread
class residing in the System.Threading namespace. The Thread class represents a thread. A thread is the
smallest unit of processing. When you run any application, the application code runs on a thread. Threads
are handled by the operating system, and most modern programming languages provide classes that
Search WWH ::

Custom Search