Java has multithreading programming feature which is nothing but a form of multitasking. Multitasking enables a system to perform multiple tasks simultaneously.
The main aim of this concept is to reduce the response time and increase the throughput of a processor.
It is done by allocating a small slice of CPU time to each process and switching back and forth among these concurrently running processes, to create the illusion of parallel execution.
Multitasking can be classified into two types :
Basis | Process Based Multitasking | Thread Based Multitasking |
---|---|---|
Definition | Executing two or more programs concurrently | Executing two or more threads simultaneously |
Smallest Unit | Program | Thread |
Overhead | High | Low |
Address space | Every program has its own address space | Every thread shares a same address space |
Context switching Cost | High | Low |
Interprocess communication | limited and expensive | Inexpensive |
Also Known | Heavyweight process | Lighweight process |
Best suitable | OS level | Programmatic level |
Example | Executing MP3 player, text editor, and Browsing concurrently | Printing documents as well keep editing on a text editor |
Process-based multitasking enables a system to execute two or more programs concurrently, whereas Thread based multitasking enables a program to carry out two or more tasks in form of threads simultaneously.
In process-based multitasking, a program is the smallest unit that can be executed independently
In thread-based multitasking, a thread is the smallest unit that can be executed independently
Process-based multitasking puts more overhead than thread-based multitasking.
A thread is a lightweight process, therefore thread-based multitasking puts less overhead than process-based multitasking.
A process performs heavy tasks and takes up a separate address space
A thread performs light tasks and shares the same address space with other threads belong to the same program
Interprocess communication is limited and expensive in Process-based multitasking, whereas
Interthread communication is inexpensive in thread-based multitasking.
In the case of process-based multitasking, context switching from one process to another costs high.
In case of a thread, context switching from one thread to another cost low.
Using process-based multitasking, multiple programs such as MP3 player, text editor, and Browsing are together executed parallelly.
Using thread-based multitasking, multiple tasks of a single program such as compiler in which compiling and opening another file at the same time can be done together with the help of two threads.
A process is a program in execution. Each process has its own address space, program counter, data section, stack and register set.
A Thread is an independent dispatchable code that has a unique path of execution. Each thread has its own Program counter, stack and register set despite all share same address space.
This post was last modified on October 11, 2020