Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Overview of Multithreading Concept in Java

[post-views]

 

 

In this lesson, you will learn.

  • Overview of Multithreading
  • What is Multiprogramming
  • What is Multitasking

 

Java provides built-in support for multithreaded programming and all the class libraries
are designed with multithreading in mind
.

 

1. What is Multithreading

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.

 

What is a Thread?

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.

 

Example 1: Office Applications

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.

 

Example 2: Creating Multiple Tabs of a Web Browser

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.

 

 

2. What is Multiprogramming

  1. Multiprogramming is a method that allows multiple programs to run on a single processor at the same time.
  2. In multiprogramming, system resources (such as CPU, disk, and memory) are utilized efficiently by overlapping the demands of multiple processes.

 

Example

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.

 

 

3. What is Multitasking

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.

 

 

3.1 Process-Based Multitasking

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.

 

3.2 Thread-Based Multitasking

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.

 

Important Points!

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.

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

 

 

Submit a Review

 

 

Layer 1
Login Categories