I am here to continue the discussion around Threading. Today we will explain Multithreading and some concepts around that.
In case, you have not had a look at our first article, go through the following link:
Threading Simplified: Part 1
Before multithreading, let's talk about some other related concepts.
Multiprogramming
It is the core concept behind multithreading today. Multiprogramming is the ability to load multiple programs into memory and execute them when required. The term multiprogramming is usually used in single processor environments.
The idea of multiprogramming arises from a notion to keep the CPU busy as much as possible. To understand how it works, you can see an example when a program or Job1 is awaiting I/O and then Job2 takes the CPU time and so on to keep the CPU busy.
Multitasking
Multitasking is a natural extension of multiprogramming where all the non-blocked or active jobs are allocated the time on a sharing basis. Multitasking helps to mimic the parallelism by allocating time to all the active jobs.
There are various kinds of algorithms available for time scheduling, for example, Round robin and FIFO.
These days most computers or even smaller devices such as smart phones and tablets are designed to support multitasking. The simplest example of multitasking is running media players, typing emails in Outlook and at the same time, responding to someone in the Lync.
In the following example, Job 2, Job 3 and though to Job n are allocated a time slice based on the selected time scheduling algorithm.
Multiprocessing
Multiprocessing is the ability of having more than one CPU in a computing machine. Having more than one CPU Core helps to do true parallelism that in turn enhances the performance of calculation-intensive applications.
These days most computers or even smaller devices are loaded with more than one CPU processor or core.
The following example depicts the multiprocessing environment where two CPUs have their own set of jobs to execute.
Now let's talk about multithreading.
Multithreading is a further extension of multitasking where a task or process is again divided into threads.
In a multithreaded environment, threads share the resources of processes but can be executed independently.
Multithreading helps to do abstract concurrent execution and when added with multiprocessing, the execution power multiples and true parallelism can be done.
The example of multithreading could be automatic spell check when writing content in a Word document.
In the following example, you can see that Job 1 is further divided into multiple threads.
There are various benefits of multithreading, including faster execution, responsiveness, better resource utilization and so on. However we need to be careful about thread synchronization issues when developing applications.
There are various kinds of methods available to handle synchronization issues, we will talk about them in future articles.
I hope you liked the article, please share your thoughts/comments.