Java Multithreading Tutorial in Depth | Realtime Example

In the previous tutorial, you learned the basic concept of thread in Java. Now we will learn multithreading in Java with realtime examples in depth.

We know that Java is a multithreaded programming language, which means we can develop a multithreaded program using Java.

Multi-threading programming approach is one of the most powerful features of Java. It makes our program more responsive and interactive as well as improves performance.

To grasp the concept of multithreading, one has to understand the meaning of multithreading.

What is Multithreading in Java

Multithreading means multiple threads of execution concurrently. The process of executing multiple threads simultaneously (concurrently) is called multithreading in Java.

In other words, multithreading is a technique or programming concept in which a program (process) is divided into two or more subprograms (subprocesses), each of which can perform different tasks simultaneously (at the same time and in parallel manner). Each subprogram of a program is called thread in Java.

Look at the below figure where a Java program has three threads, one main and two others. The other two threads, ThreadA and ThreadB are created and started from the main thread.

When a program contains multiple flows of control, it is called multithreaded program.Multithreaded program in Java

Once initiated by the main thread, thread ThreadA and ThreadB run simultaneously and share the resources together. It is the same as people living in a joint family and sharing certain resources among all of them.

When a program contains more than one thread, the CPU can switch between two threads to execute them at the same time. The switching between two threads is known as context switch.

The switching from one thread to another thread occurs so fast that it appears to the users that all threads are being executed at the same time. But in reality, only one thread is executed at a time.

This technique is useful for those applications which need multiple tasks to be done simultaneously. In a single processor system, multiple threads share CPU time that is known as time-sharing.

The operating system is responsible for allocating and scheduling resources to them. Thus, multithreading improves the performance of CPU by maximum utilization and keeping the idle time of CPU to minimum.

In a multithreaded program, each thread is assigned a single task to perform and executes independently. If an exception occurs in one thread, it does not affect other threads during the execution.

For example, one thread read data, another thread process it, and third thread write it, thus improves the overall performance of an application.

We can write a multithreaded program to display animations, to play music, and download a file from the network at the same time.

Advantage of Multithreading in Java

The advantages of using multithreading programming concept are as follows:

1. In a multithreaded application program, different parts of the application are executed by different threads. The entire application does not stop even if an exception occurs in any of the threads. It does not affect other threads during the execution of the application.

2. Different threads are allotted to different processors and each thread is executed in different processors in parallel.

3. Multithreading helps to reduce computation time.

4. Multithreading technique improves the performance of the application.

5. Threads share the same memory address space. Hence, it saves memory.

6. Multithreaded program makes maximum utilization of CPU and keeping the idle time of CPU to minimum.

7. Context switching from one thread to another thread is less expensive than between processes.

Drawbacks of Multithreading in Java

The drawbacks of multithreading are as follows:

1. Increased complexity.
2. Synchronization of shared resources.
3. In the multithreading programming concept, debugging is difficult. At times, result is unpredictable.
4. Potential deadlocks.
5. Programming complications may occur.

Multitasking in Java

The process of executing one or more tasks concurrently or at the same time is called multitasking. It is the ability of an operating system to execute multiple tasks at once. The main purpose of multitasking is to use the idle time of CPU.

Multitasking can be implemented in two ways:

1. Process-based multitasking (Multiprocessing)
2. Thread-based multitasking (Multithreading)

Process-based Multitasking (Multiprocessing)

The process of executing multiple programs or processes at the same time (concurrently) is called process-based multitasking or program-based multitasking. In process-based multitasking, several programs are executed at the same time by the microprocessor.

Therefore, it is also called multiprocessing in Java. It is a heavyweight. A process-based multitasking feature allows to execute two or more programs concurrently on the computer.

A good example is, running spreadsheet program while also working with word-processor.

Each program (process) has its own address space in the memory. In other words, each program is allocated in a separate memory area.

The operating system requires some CPU time to switch from one program to another program. The switching of CPU among programs is called context switching.

The switching from one program to another program is so fast that it appears to the user that multiple tasks are being done at the same time.

Thread-based Multitasking (Multithreading)

A thread is a separate path of execution of code for each task within a program. In thread-based multitasking, a program uses multiple threads to perform one or more tasks at the same time by a processor.

That is, thread-based multitasking feature allows you to execute several parts of the same program at once. Each thread has a different path of execution.

Realtime Example of Multithreading in Java

Let’s take different examples of thread-based multithreading in Java.

1. A very good example of thread-based multithreading is a word processing program that checks the spelling of words in a document while writing the document. This is possible only if each action is performed by a separate thread.

2. Another familiar example is a browser that starts rendering a web page while it is still downloading the rest of page.

Consider a program as shown in the below figure. The program is divided into two parts. These parts may represent two separate blocks of code or two separate methods that can perform two different tasks of the same program.Thread based multitasking (Multithreading) in Java

Hence, a processor will create two separate threads to execute these two parts simultaneously. Each thread acts as an individual process that will execute a separate block of code.

Thus, a processor has two threads that will perform two different tasks at a time. This multitasking is called thread-based multiple tasking.

Advantage of Thread-based Multitasking over Process-Thread Multitasking

The main advantages of thread-based multitasking as compared to process-based tasking are

1. Threads share the same memory address space.

2. Context switching from one thread to another thread is less expensive than between processes.

3. The cost of communication between threads is relatively low.

4. Threads are lightweight as compared to processes (heavyweight). They utilize the minimum resources of the system. They take less memory and less processor time.

Java supports thread-based multitasking and provides a high quality of facilities for multithreading programming.

A great way to remember the difference between process-based multitasking and thread-based multitasking is process-based multitasking works with multiple programs or processes whereas thread-based multitasking works with parts of one program.

In process-based multitasking, a program is the smallest unit of executable code whereas, in thread-based multitasking, thread is the smallest unit of executable code.

In this tutorial, we have covered multithreading in Java in depth with realtime examples. Hope that you will have understood the basic concept of multithreading and multitasking in Java.

If you get any errors in this tutorial, please inform our team through email. Your email will be precious to us.
Thanks for reading!!!
Next ⇒ Thread Class in Java⇐ PrevNext ⇒

Please share your love