Mastering Multithreading in Java: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-04 04:46:26   8492  Share
Subject Syllabus DetailsSubject Details 5 Questions
☰ TContent
☰Fullscreen

Table of Content:

In concurrent programming, there are two basic units of execution: processes and  threads.  In the Java programming language, concurrent programming is mostly concerned with threads. 

A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process).

It is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. Each part of such program is called a thread. So, threads are light-weight processes within a process.

Multiprocessing and multithreading, both are used to achieve multitasking But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Java Multithreading is mostly used in games, animation etc..

So, what is thread in short

A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution. they are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.

At least one process is required for each thread.

life cycle of thread in java

Understanding process and thread

Process Thead
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space. Threads are sometimes called lightweight processes. Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process.
 To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources, such as pipes and sockets. Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication.
Most implementations of the Java virtual machine run as a single process. A Java application can create additional processes using a ProcessBuilder object.  Every application has at least one thread — or several if you count "system" threads that do things like memory management and signal handling. But from the application programmer's point of view, you start with just one thread, called the main thread. This thread has the ability to create additional threads, 

Advantages of Java Multithreading

1) It doesn't block the user because threads are independent and you can perform multiple operations at same time.

2) You can perform many operations together so it saves time.

3) Threads are independent so it doesn't affect other threads if exception occur in a single thread.

Multitasking

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:

  • Process-based Multitasking(Multiprocessing)
  • Thread-based Multitasking(Multithreading)
Multiprocessing Multithreading
It is Process-based Multitasking It is Thread-based Multitasking

The process is heavyweight.

Thread is lightweight.

Each process has its own address in memory i.e. each process allocates separate memory area.

Threads share the same address space.

Cost of communication between the process is high.

Cost of communication between the thread is low.

Switching from one process to another requires some time for saving and loading registers, memory maps, updating lists etc.

No Switching