Threads play a major role in applications programming today. For example, most Web servers are threaded, as are many Java GUI programs. Here are the major settings in which using threads has been founded convenient and/or efficient:
• Programs with asynchronous events:
Here the program must be ready for various events, but does not know the order in which they might occur. For example, in Sections 3.1 and 3.2, we have a network server connected to several clients. The server does not know from which client the next message will arrive. So, we have the server create a separate thread for each client, with each thread handling only its client.
• Programs whose peformance can be improved through latency hiding:
Here the program is doing multiple I/O operations, each having long latency, i.e. delay in response. We’d like to perform useful work while waiting for the response, so we have different threads for each I/O action. This way, although the latency is still there, it is “hidden” by doing other useful work in parallel.
For example, in Section 4.2, each thread performs a separate network operation.
• Computation-intensive programs:
If our program is a long-running mathematical computation, it can really benefit from having several processors, e.g. two processors in the case of dual-core machines. By having our program set up a different thread for each processor, we have the potential for substantial speedup, due to the parallelization of the computation. An example is in Section 5.

What Are Threads?
Processes
If your knowledge of operating systems is rather sketchy, you may find this section useful. Modern operating systems (OSs) use timesharing to manage multiple programs which appear to the user to be running simultaneously. Assuming a standard machine with only one CPU, that simultaneity is only an illusion, since only one program can run at a time, but it is a very useful illusion. This is changing, as for example dual-core CPU chips have become common in home PCs. But even then, the principle is the same, as there typically will be more processes than CPUs, so that some of the simultaneity is illusory. Each program that is running counts as a process in Unix terminology (or a task in Windows). Multiple copies of a program, e.g. running multiple simultaneous copies of the vi text editor, count as multiple processes. The processes “take turns” running, of fixed size, say for concreteness 30 milliseconds. After a process has run for 30 milliseconds, a hardware timer emits an interrupted which causes the OS to run. We say that the process has been pre-empted. The OS saves the current state of the interrupted process so it can be resumed later, then selects the next process to give a turn to. This is known as a context switch; the context in which the CPU is running has switched from one process to another. This cycle repeats. Any given process will keep getting turns, and eventually will finish. A turn is called a quantum or timeslice.

Download pdf Tutorial on Threads Programming with Python