5 reasons to learn multithreading
Multithreading is a CPU feature that allows you to execute multiple sets of instructions at once, independently, sharing the same memory resources. For example, if you look at an application like Microsoft Excel, it has to take user inputs, as well as keep performing computations in the background. If that is done by a single threaded process, we will not be able to interact with the application till the internal processes are completed, at all. This is why multithreading is used in almost all computer and mobile applications today.
Here are five reasons to learn multithreading if you are a programmer.
UI responsiveness
Just like MS Excel, this reason applies to almost all computer and mobile applications. Responsiveness is the biggest factor for usability. If for each click of a mouse or each data entered, if user has to wait for 5 seconds, then user will hate the app and switch to any other available alternative.
Even when the application is loading, if user isn’t made aware of the progress, then user thinks the application is hung. So, UI should be running on its own thread, and any other significant computation should not be done in UI thread.
Task breakdown
Lets say you have an array {1, 2, 3, 4, 5}, and lets say you want to multiply each element by 2. Simple, right? Things get done in a blink of an eye. Now lets think your array has 10 million elements. Not so easy now? multithreading comes to rescue in such situations. If you can divide the problem so that the sub-problems are independent, multithreading approach can be taken.
If you break the problem into two halves, the execution time becomes approximately half (minus the fractional amount of time needed for thread creation and initialization). If you know the divide and conquer approach to sorting problem, you can relate to it.
CPU Utilization
Nowadays, hardware has become so much better. On an average, the processor speeds have been doubling every 4 years. Processors don’t want to handle single computations anymore.
When you do normal work in your computer, you see that less than 50% of the CPU gets utilized. What does this mean? Your computer has the ability to handle twice the tasks you are doing now. Multithreading means that you will be better utilizing the CPU when you can.
Performance
Combined impact of task breakdown and CPU utilization is the performance. Imagine your application handling millions of data points in seconds, vs. minutes without multithreading. It certainly makes your clients nod their heads. Better performance means better usability and more clients.
Career growth
The above advantages of multithreading and concurrency mean that this skill is very much in demand, and if you as a programmer have it in your quiver, you will definitely be in demand.
There are however some things that need to be kept in mind if you are using multithreading.
Race condition is a common problem of multithreading. If two threads are operating on same set or dependent sets of data, the output depends on which thread operates first. There are useful tools to overcome this issue though (ex. mutex).
Threads creating does come with a small cost. And how many threads run at a time depends on the hardware and operating system combination. When creating more threads, its best to go through these limitations.
Multithreaded program is harder to debug than single threaded program as the control flow is not line by line. This makes the programmer’s life difficult if code is not organized well. The other issue with multithreading is the impact on the CPU. CPU nearing its hardware limits leads to problems such as overheating. One must be aware of implications like this and balance the number of threads.
Multithreading is a great tool which comes with its advantages and limitations. When a programmer understands the pros and cons and uses this tool optimally, it gives good results.