[post-views]
In this lesson, you will learn.
Multithreading in Java is a feature that allows concurrent execution of two or more parts of a program.
Each part of such a program is called a thread and is the smallest processing unit also known as a lightweight processes available within a process.
Therefore, multithreading leads to maximum utilization of the CPU for multitasking.
Multithreading is used to perform complicated tasks in the background without interrupting the main program.
The thread is the smallest execution unit which consists of its program counter, a stack, and a set of registers.
Threads are known as lightweight processes.
As each thread has its own independent resource for process execution, multiple processes can be executed parallelly by increasing the number of threads.
There is a context switching between threads which gives illusions that the threads are running in parallel.
Let’s understand with the help of examples.
Office applications like Microsoft Word or Google Docs use multithreading to enhance user experience.
For example, while the main thread handles user input and display, the background threads perform tasks like spell-checking, auto-saving documents, and updating the user interface concurrently without interruptions.
The below Figure shows how the main thread is divided into multiple parts or threads (known as child threads) and is executed concurrently to perform multiple tasks.
Here, one thread handles the spell checking in the background, the second thread saves the text to file periodically, and another thread updates the user interface without interruptions and executes concurrently.
This allows the application to utilize resources efficiently without making the user wait for one task to complete before starting another.
Web browsers like Google Chrome, Firefox, and Safari use multithreading extensively to improve responsiveness and performance.
Each tab in a modern web browser can run as a separate thread, allowing users to browse different websites in different tabs simultaneously without one tab’s loading process affecting others.
Consider a scenario where several applications like a web browser, a music player, and a word processor are running on the same computer.
Even though there’s only one CPU, the operating system allocates time slices to each program.
While the music player is waiting for the next song to buffer, the CPU can process a user’s input in the word processor.
This makes it appear as though all programs are running at the same time.
Multitasking refers to the ability of an operating system to perform multiple tasks (also called processes) at the same time. The tasks can be either programs or threads.
A process is, in essence, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently.
It is a specialized task of multiprogramming.
For example, process-based multitasking enables you to run the Java compiler at the same time that you are using a text editor or visiting a website.
In process-based multitasking, a program is the smallest unit of code that the scheduler can dispatch.
In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code.
This means that a single program can perform two or more tasks simultaneously using a thread.
For example, a text editor can format text at the same time while it is printing, as long as these two actions are being performed by two separate threads.
Multitasking threads require less overhead than multitasking processes. Processes are heavyweight tasks that require their own separate address spaces.
Interprocess communication is expensive and limited. Context switching from one process to another is also costly.
Threads, on the other hand, are lighter weight. They share the same address space and cooperatively share the same heavyweight process.
Interthread communication is inexpensive, and context switching from one thread to the next is lower in cost.
Multithreading enables you to write efficient programs that make maximum use of the processing power available in the system and is achieved by keeping idle time to a minimum.