@techwithvishalraj
Java
Multithreding
Introduction
@techwithvishalraj
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.
However, we use multithreading than multiprocessing because
threads use a shared memory area. They don't allocate separate
memory area so saves memory, and context-switching between the
threads takes less time than process.
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:
1) Process-based Multitasking (Multiprocessing)
2) Thread-based Multitasking (Multithreading)
@techwithvishalraj
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.
At least one process is required for each thread.
Program vs Process vs Thread
@techwithvishalraj
Program:
A program is an executable file containing a set of instructions
passively stored on disk.
Process:
A process is a program in 𝐞𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧.
When a program is loaded into the memory and becomes active,
the program becomes a process.
A process requires some essential resources such including CPU
time, program counter, stack, memory, files, and I/O devices — to
accomplish its task.
Program is a 𝐩𝐚𝐬𝐬𝐢𝐯𝐞 entity while process is an 𝐚𝐜𝐭𝐢𝐯𝐞 entity.
One program can have 𝐦𝐮𝐥𝐭𝐢𝐩𝐥𝐞 processes.
Thread :
A 𝐓𝐡𝐫𝐞𝐚𝐝 is the smallest unit of execution within a process (or)
basically it is a segment of a process .
Thread is also known as lightweight process.
There are two types of processes :
1. 𝐒𝐢𝐧𝐠𝐥𝐞 𝐓𝐡𝐫𝐞𝐚𝐝𝐞𝐝 𝐏𝐫𝐨𝐜𝐞𝐬𝐬
2. 𝐌𝐮𝐥𝐭𝐢 Threaded Process
@techwithvishalraj
Program vs Process vs Thread
As shown in the 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.
Program (Disk) process (RAM)
Threads
TID - Thread id
Note: Install process
explorer to see the threads
and process.
https://process-explorer
Program vs Process vs Thread @techwithvishalraj
Instruction 1
Disk
Instruction 2
Instruction 3
Program
......
Instruction n
Process Process Process
RAM
Code Data Files Code Data Files
Register Register Register Register
Counter Counter Counter Counter
Stack
Stack Stack Stack
Thread 0 Thread 1 Thread 2 Thread
𝐌𝐮𝐥𝐭𝐢 𝐓𝐡𝐫𝐞𝐚𝐝𝐞𝐝 𝐏𝐫𝐨𝐜𝐞𝐬𝐬 𝐒𝐢𝐧𝐠𝐥𝐞 𝐓𝐡𝐫𝐞𝐚𝐝𝐞𝐝 𝐏𝐫𝐨𝐜𝐞𝐬𝐬
@techwithvishalraj
𝐒𝐢𝐧𝐠𝐥𝐞 𝐓𝐡𝐫𝐞𝐚𝐝𝐞𝐝 𝐏𝐫𝐨𝐜𝐞𝐬𝐬
A single thread executes the instructions line by line from beginning
to end.
A single-threaded process has one program counter specifying the
next instruction to execute.
The execution of such a process must be sequential — the CPU
executes one instruction after another, until the process completes.
𝐌𝐮𝐥𝐭𝐢 Threaded Process
Multithreading is a model of program execution that allows for
multiple threads to be created within a process, executing
independently but concurrently sharing the process resources like
data, memory, resources, files, etc with their peer threads within a
process.
Each thread has it’s own its own stack, register and program
counters .
Threads can directly communicate with each other as they share the
same address space.
@techwithvishalraj
How to create a Thread
A 𝐓𝐡𝐫𝐞𝐚𝐝 is the smallest unit of execution within a process (or)
basically it is a segment of a process .
Thread is also known as lightweight process.
There are two ways to create a thread:
1. By extending the Thread class
2. By implementing Runnable interface.
@techwithvishalraj
1. By Extending the Thread Class
Thread class:
Thread class provide constructors and methods to create and
perform operations on a thread. Thread class extends Object class
and implements Runnable interface.
Commonly used Constructors of Thread class:
1. Thread()
2. Thread(String name)
3.Thread(Runnable r)
4. Thread(Runnable r, String name)
Output:
i= 0
class CustomThread extends Thread { i= 1
public void run() { i= 0
for (int i = 0; i < 10; i++) { i= 1
System.out.println("i= " + i); i= 2
} i= 3
} i= 2
} i= 4
public class ByExtendingThreadClass { i= 5
public static void main(String[] args) { i= 6
CustomThread t1 = new CustomThread(); i= 7
t1.setName("CustomThread"); i= 3
t1.start(); i= 4
CustomThread t2 = new CustomThread(); i= 8
t2.start(); i= 9
} i= 5
} i= 6
i= 7
i= 8
i= 9
2. By Implementing the Runnable Interface @techwithvishalraj
Another way to create a thread is by implementing the Runnable
interface. This is preferred when your class is already extending
another class since Java does not support multiple inheritance.
If you are not extending the Thread class, your class object would
not be treated as a thread object. So you need to explicitly create
the Thread class object. We are passing the object of your class that
implements Runnable so that your class run() method may execute.
Output:
i= 0
class CustomRunnable implements Runnable{
i= 1
public void run() {
i= 0
for (int i = 0; i < 10; i++) {
i= 1
System.out.println("i= " + i);
i= 2
}
i= 3
}
i= 2
}
i= 4
public class ByImplementingRunnableInterface {
i= 5
public static void main(String[] args) {
i= 6
Thread t1 =new Thread(new CustomRunnable());
i= 7
t1.start();
i= 3
Thread t2 =new Thread(new CustomRunnable());
i= 4
t2.start();
i= 8
}
i= 9
}
i= 5
i= 6
i= 7
i= 8
i= 9
@techwithvishalraj
Thank
you!
vishal-bramhankar
techwithvishalraj
Vishall0317