Multithreading in Java
Multithreading in Java is a process of executing multiple threads simultaneously. A
thread is a lightweight sub-process, the smallest unit of processing.
Multiprocessing and multithreading, both are used to achieve multitasking.
Advantages of Java Multithreading
It doesn't block the user because threads are independent and you can
perform multiple operations at the same time.
You can perform many operations together, so it saves time.
Threads are independent, so it doesn't affect other threads if an exception
occurs 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 in two ways:
Process-based Multitasking (Multiprocessing)
Thread-based Multitasking (Multithreading)
Process-based Multitasking (Multiprocessing)
Each process has an address in memory. In other words, each process
allocates a separate memory area.
A process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another requires some time for saving and
loading registers, memory maps, updating lists, etc.
Thread-based Multitasking (Multithreading)
Threads share the same address space.
A thread is lightweight.
Cost of communication between the thread is low
What is Process in Java?
When we write a group of statements in a program, these statements are
executed by JVM one by one in the order they appear. This default execution
process happens through the main thread in Java.
Thread in Java is the smallest unit of executable code in a program. It helps
to divide a program into multiple parts to speed up the process.
A process is a program that executes as a single thread. In other words, when an
executable program is loaded into memory, it is called process.
What is Thread in java
A thread is a lightweight subprocess, the smallest unit of processing. It is a
separate path of execution.
Threads are independent. If there occurs exception in one thread, it doesn't
affect other threads. It uses a shared memory area
Every individual process has its own separate memory address space and
can execute a different program.
Each process can have more than one thread.
Each process communicates through the operating system, files, and
network.
As shown in the above figure, a thread is executed inside the process. There is
context-switching between the threads. There can be multiple processes inside the
OS, and one process can have multiple threads.
Note: At a time one thread is executed only
Java Thread class
Java provides Thread class to achieve thread programming. Thread class provides
constructors and methods to create and perform operations on a thread. Thread
class extends Object class and implements Runnable interface.
Life Cycle of a Thread
A thread goes through various stages in its life cycle. For example, a thread is
born, started, runs, and then dies. The following diagram shows the complete life
cycle ofa thread
States of a Thread Life Cycle in Java
Following are the stages of the life cycle
New − A new thread begins its life cycle in the new state. It remains in this
state until the program starts the thread. It is also referred to as a born thread.
Runnable − After a newly born thread is started, the thread becomes
runnable. A thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the
thread waits for another thread to perform a task. A thread transitions back
to the runnable state only when another thread signals the waiting thread to
continue executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a
specified interval of time. A thread in this state transition back to the
runnable state when that time interval expires or when the event it is waiting
for occurs.
Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.