Modern applications are built in the way that they can perform well while consuming fewer resources. In the old days, we face hanging out of the computer. The reason for hanging out of the computer was that if one program stops working then the whole computer stops working. The reason for hanging out was also of using single core processor. Nowadays computer is divided into hundreds of types. Some examples of today computer are iWatch, smartphone, car DVD player, touch screen devices, hospital machines, body sensors, modern cameras. These all devices are small computers.
With the invention of new devices, programming languages have also become intelligent. Java is an example of a programming language that is vastly used all over the world. Java is also multithreaded language.
There are two types of programmes written in Java. One program is without using threads and another program is by using threads. If we run the same program with and without including threading then the speed of program differs. Thread program divides programming code into parts and waiting time in the program is converted into execution. Java program supports two types threading. One is user-level threads and other is kernel level threads. User level threads do not need operating system interaction while kernel-level threads are controlled and managed by operating system.
The thread is also known as a small task of the program. Let me give you a real-world example.
In the above picture. One woman is doing two tasks at a time. She is taking care of the baby and using her laptop. Similarly, in the computer, one program is running multiple tasks. If we start our computer, then we open different programs like chrome, excel sheet, calculator etc. All these programs run smoothly on our computer. There are different tasks running in each program. Like in chrome browser, we have extensions installed that run in parallel within the browser. So chrome browser running multiple threads at the same time. All new software support multithreading. As you get a little idea about multithreading, we can now discuss some advantages and disadvantages of multithreading.
Advantages of multithreading system
- Responsive: As there are different threads running in each program then if one thread stops working then other threads keep the program running and response to the user quickly.
- Resource utilization: Idle time of CPU becomes less as there are several threads running in each program.
- Parallel execution: In multiprocessor multiple tasks/threads of the program complete in parallel.
- Large programs run smoothly: If you have a high-end processor with multicore inside processor then large programs run without any problem. In complex GUI apps there exist many threads and each thread performs its own work. So without interfering with the operating system, large programs perform well. If any thread stops responding then other thread take care of it.
- Data sharing: Threads within a program share same data with each other so there is no extra space required for each thread.
- Improve cache usage, throughput and turnaround time.
- Handling multiple users: In networking of computer, multithreading plays an important role. It speeds up communication and processing of data.
- Creating and managing threads is easy as compared to big processes.
- Context switching is fast if we compare it with stand-alone processes.
Disadvantages of multithreading system
- Security: As threads within a program share same data so if any unknown thread comes then it can change data.
- Thread Cloning: If one thread wants to clone then other threads in a program needs to reassign data
- Blocking program: Suppose there is single kernel thread handling the whole program, if that kernel thread stops then it will result in blocking the whole program.
- Slow down the system: If there is single core processor running then multithreading slows down the computer as there become a heavy load on the single processor.
Example of multithreading systems
All latest operating systems are multithreaded