A thread is simply a sequence of programmed instructions that can be executed independently in CPU. A single process of a program can have several threads, these threads are executed parallelly at the same time within the same process.
There are two type of processes based on the number of threads.
- Single-threaded processes. :- Only one task is executed.
- Multi-threaded processes. :- Multiple tasks can be executed parallelly.

This figure shows block diagrams for multi-thread and single-threaded processes.
Why we need Threads.
A process of a program may have more than one tasks to be executed at the same time, creating a new process for each of these tasks is time-consuming and resource-intensive. For example
A web server accepts client requests for web pages, images, sound, and so forth. A busy web server may have several (perhaps thousands of) clients concurrently accessing it. Each of these clients needs to work independently.
If the webserver is single-threaded and creates new processes for each of these clients, then the clients have to wait until the process creation finishes ( a process creation needs more resources Ex:- a different data set, code, file…etc ) which is much slower when there are multiple clients are requesting the server.
If the web-server process is multi-threaded, the server will create a separate thread that listens for client requests. When a request is made, rather than creating another process, the server creates a new thread(using the same data set, file, code..etc) to service the request and resumes listening for additional requests.

Motivation
Most software applications that run on modern computers and mobile devices are multithreaded. An application typically is implemented as a separate process with several threads of control. Below we highlight a few examples of multithreaded applications:
- An application that creates photo thumbnails from a collection of images may use a separate thread to generate a thumbnail from each separate image.
- 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.
In a multi-core computer( core i3, i7, i5….etc), multithreaded programming provides a mechanism for more efficient use of these multiple computing cores and improved concurrency.
concurrency and parallelism
Both the concurrency and parallelism are the different mechanisms that are used to accomplish multi-threading. In a single-core processor, the parallelism mechanism can not be used.
| Concurrency. | Parallelism. |
| multiple tasks which start, run, and complete in overlapping time periods, in no specific order is simply the concurrency. e.g . on a single-core processor. | Parallelism is when multiple tasks OR several parts of a unique task literally run at the same time, e.g. on a multi-core processor. |
Types of Parallelism
There are two types of parallelisms, Data parallelism and Task parallelism.
Data parallelism:-
Data parallelism focuses on distributing subsets of the same data across multiple computing cores and performing the same operation on each core.
Ex:- Searching a word in a PDF document.
Suppose you have a PDF book with 1000 pages, now you want to find a word in the book. Sorting the whole document sequentially is not efficient. What the program does for you to give a quick result is Data parallelism. The whole document will be divided by 200 pages and assign to 5 different processing cores to do the searching. Now 1000 pages will be sorted within 200 pages time.
Here the operation is same, and the data set has been distributed across different processing cores.
Task parallelism:-
Task parallelism involves distributing not data but tasks (threads) across multiple computing cores. Each thread is performing a unique operation.
Ex:- In a word processor software, one thread is used to display what you type whereas another thread is used to check your spelling.
Here two different tasks are distributed across two processing cores.

Thread Libraries
A thread library provides the programmer with an API for creating and managing threads. Three main thread libraries are in use today: POSIX Pthreads, Windows, and Java. Among these three thread libraries, the widely used library by the programmers is Java.