Java – Multithreading
Before learning Multithreading in Java let’s see, what is Multitasking?
Running two or more tasks simultaneously is known as multitasking.
Multitasking consists of two types:
1) Process based Multitasking.
2) Thread based Multitasking.
1) Process based Multitasking
Running two or more programs/applications simultaneously on a single machine is called process based multitasking.
In our day to day life, often we run multiple programs on a computer. Like, we may open Game Application, Audio Player, etc., at the same time, so that we can listen to music while playing a game. All these processes execute simultaneously and independently of each other. Therefore it is process based multitasking.
- It is used at an Operating System level.
- Process based multitasking is heavy weight because it uses separate address spaces for each process.
- In process based multitasking, due to separate address spaces for each process, context switching (task switching) is difficult.
2) Thread based Multitasking
Running multiple tasks (parts of the program) simultaneously within the same program (process) is known as thread based multitasking.
When we open some video player, we can watch the video, listen audio, can change the size of a screen, etc., simultaneously. Here video player is one program (application), and it works with video, audio, screen size, etc. properties simultaneously. These properties are the part of a single program (application) that can run simultaneously and independently. Therefore it is a thread based multitasking.
Another example of thread based multitasking application is Microsoft Word. While writing in Microsoft Word, it can print the letters that we press on keyword as well as it can check spelling mistakes and correct them. These all processes are done simultaneously but are independent of each other, because here each process is a separate independent thread.
- It is used at the Programming level.
- Process based multitasking is lightweight because it uses same address space for each part of the program.
- As it shares the same space for each part of program context switching is easy.
Now let’s see Java Multithreading in detail.
A thread is a part of the program that can run separately without depending on other parts of the program.
The program which contains two or more threads (part of programs), that can run separately and simultaneously without depending on each other is called multithreaded program.
Multithreading/Multitasking is mainly used to increase the performance of application/program/system because it makes all the required processes execute simultaneously instead of executing them one by one. It helps to keep the idle time of CPU/processor minimum.
For example, if we are working with some text editor, it is impossible for us to feed input from the keyboard to CPU with the speed which CPU processes. Thus, CPU remains idle for most of the time. Instead of that, we can use CPU for other work in its idle time, resulting in multiple tasks at the same time. For example, we can download some file and listen to some song simultaneously while working with a text editor. Hence multithreading/multitasking helps to reduce idle time of CPU and increase the performance of the system.
Consider that Microsoft Word is not a multithreaded but a single threaded. Therefore it will complete only one task/process at a time. First, it will take only inputs from the keyboard for each letter. Then it will print all letters one by one. Then it will check spelling mistakes. Then correct it. Then check grammar mistakes. Then correct it. Hence it will take a long time to complete all these processes and will become impossible to work with Microsoft Word. But with multithreading, it does all the process simultaneously, which reduces the performance time and increase the speed of execution as well as making it possible to work with MS Word very easily.
Although multithreading is very powerful and important feature to create potent programs and enhance their speed, it should be used carefully.
Creating too many threads in the program may reduce the speed of a program instead of increasing because creating more threads will result in more contexts switching, which will spend more and valuable time of CPU in context switching rather than executing the program.