- An application typically is implemented as a separate process with several threads of control.
- A web browser might have one thread display images or text while another thread retrieves data from the network.
- A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background.
Left: A word processor with three threads. Right: A multithreaded web server.
- In certain situations, a single application may be required to perform several similar tasks.
- A busy web server may have several (perhaps thousands) of clients concurrently accessing it.
- When the server receives a request, it creates a separate process to service that request.
- Process creation is time consuming and resource intensive.
- In fact, this process-creation method was in common use before threads became popular.
- Consider the Web server could be written in the absence of threads. The net result is that many fewer requests/sec can be processed.
- The better approach would multithread the web-server process. Thus threads gain considerable performance.
- The server would create a separate thread that would listen for client requests;
- when a request was made, rather than creating another process, the server would create another thread to service the request.
- Many OS kernels are now multithreaded; several threads operate in the kernel, and each thread performs a specific task, such as managing devices or interrupt handling.