SlideShare a Scribd company logo
Multi-Threading 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.
• 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.
• Java Multithreading is mostly used in games, animation, etc.
Advantages of Java Multithreading
• 1) It doesn't block the user because threads
are independent and you can perform
multiple operations at the 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 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)
1) 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.
2) Thread-based Multitasking
(Multithreading)
• Threads share the same address space.
• A thread is lightweight.
• Cost of communication between the thread is
low.
What Is A Thread?
• A thread is a lightweight unit of execution within a
process.
• It has its stack of memory and can run
independently of other threads in the same process.
• Threads share the same process resources, such as
the heap and the code section.
• The Java Virtual Machine allows an application to
have multiple threads of execution running
concurrently.
• Every thread has a priority.
• Threads with higher priority are executed in preference to
threads with lower priority.
• Each thread may or may not also be marked as a daemon.
• When code running in some thread creates a new Thread
object, the new thread has its priority initially set equal to
the priority of the creating thread and is a daemon thread if
and only if the creating thread is a daemon.
• When a Java Virtual Machine starts up, there is usually a
single non-daemon thread (which typically calls the method
named main of some designated class).
Thread States And LifeCycle
• NEW: A thread that has not yet started is in this state.
• RUNNABLE: A thread executing in the Java virtual machine is
in this state.
• BLOCKED: A thread that is blocked waiting for a monitor lock
is in this state.
• WAITING: A thread that is waiting indefinitely for another
thread to perform a particular action is in this state.
• TIMED_WAITING: A thread that is waiting for another thread
to act for up to a specified waiting time is in this state.
• TERMINATED: A thread that has exited is in this state.
Below is the thread lifecycle:
• New (or Created): A thread is created by instantiating
the Thread class. At this point, it is in the new state.
• Thread myThread = new Thread();
• 2. Runnable (or Ready): After the thread is created, it
moves to the runnable state when the start() method is
called. The start() method internally calls the run()
method, and the thread becomes ready for execution.
• myThread.start(); // Moves the thread to the runnable
state
• Running: Once the scheduler selects the
thread for execution, it enters the running
state. The run() method contains the code that
will be executed when the thread is running.
• public void run() {
// Code to be executed when the thread is
running
}
• Blocked (or Waiting): A running thread may enter the
blocked state if it encounters an operation that makes it
wait, such as calling sleep(), wait(), or performing I/O
operations.
• // Example: Thread sleeps for 1 second
try {
Thread.sleep(1000); // Thread enters blocked state for 1
second
} catch (InterruptedException e) {
e.printStackTrace();
}
• Terminated (or Dead): The thread enters the
terminated state when the run() method
completes its execution or when an uncaught
exception occurs.
• // Example: Completing the run method
public void run() {
// Code to be executed when the thread is running
// ...
// The thread terminates when this method completes
}
• It’s important to note that the start() method
should be used to initiate the execution of a
thread. The actual code to be executed should be
placed in the run() method. Calling run() directly
won’t create a new thread; it will execute the
run() method in the context of the current thread.
Using start() is essential for multithreading, as it
signals the system to create a new thread and
invoke the run() method in that new thread.
Creating Threads

More Related Content

Similar to Multi-Threading in Java power point presenetation (20)

PPT
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
PPTX
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
PPT
Threads in java, Multitasking and Multithreading
ssusere538f7
 
PPTX
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
PPTX
Multithreading programming in java
Elizabeth alexander
 
PPTX
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
PPT
BCA MultiThreading.ppt
sarthakgithub
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Sachintha Gunasena
 
PPTX
Multithreadingppt.pptx
HKShab
 
PDF
Unit-3 MULTITHREADING-2.pdf
GouthamSoma1
 
PPTX
Threads in Java
HarshaDokula
 
PDF
Java Threads: Lightweight Processes
Isuru Perera
 
PPT
Chap2 2 1
Hemo Chella
 
PPT
Java
Khasim Cise
 
PPT
multithreading
Rajkattamuri
 
PPT
Java
mdfkhan625
 
PPT
Multithreading
F K
 
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
PPT
Java Multithreading
Rajkattamuri
 
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
Multithreading programming in java
Elizabeth alexander
 
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
BCA MultiThreading.ppt
sarthakgithub
 
Java threads
Prabhakaran V M
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Sachintha Gunasena
 
Multithreadingppt.pptx
HKShab
 
Unit-3 MULTITHREADING-2.pdf
GouthamSoma1
 
Threads in Java
HarshaDokula
 
Java Threads: Lightweight Processes
Isuru Perera
 
Chap2 2 1
Hemo Chella
 
multithreading
Rajkattamuri
 
Multithreading
F K
 
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
Java Multithreading
Rajkattamuri
 

More from AshokRachapalli1 (20)

PPTX
unit-1 lecture 7 Types of system calls.pptx
AshokRachapalli1
 
PPTX
DATA MODEL Power point presentation for dbms
AshokRachapalli1
 
PPTX
WEEK-2 DML and operators power point presentation
AshokRachapalli1
 
PPTX
Relational Algebra in DBMS 2025 power point
AshokRachapalli1
 
PPTX
CLOSURE OF AN ATTRIBUTE powerpontpresentatio
AshokRachapalli1
 
PPT
Relational algebra in database management system
AshokRachapalli1
 
PPT
DBMS-3.1 Normalization upto boyscodd normal form
AshokRachapalli1
 
PPTX
Data base Users and Administrator pptx
AshokRachapalli1
 
PPTX
Database Languages power point presentation
AshokRachapalli1
 
PPTX
Relational Algebra in DBMS power ppoint pesenetation
AshokRachapalli1
 
PPTX
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
PPT
ARRAYS in java with in details presentation.ppt
AshokRachapalli1
 
PPT
lecture-a-java-review .. this review ppt will help to the lectureres
AshokRachapalli1
 
PPTX
17.INTRODUCTION TO SCHEMA REFINEMENT.pptx
AshokRachapalli1
 
PPTX
joins in dbms its describes about how joins are important and necessity in d...
AshokRachapalli1
 
PPTX
6.Database Languages lab-1.pptx
AshokRachapalli1
 
PPT
Chapter5 (1).ppt
AshokRachapalli1
 
PPTX
Cache Memory.pptx
AshokRachapalli1
 
PPTX
Addressing Modes.pptx
AshokRachapalli1
 
PPTX
inputoutputorganization-140722085906-phpapp01.pptx
AshokRachapalli1
 
unit-1 lecture 7 Types of system calls.pptx
AshokRachapalli1
 
DATA MODEL Power point presentation for dbms
AshokRachapalli1
 
WEEK-2 DML and operators power point presentation
AshokRachapalli1
 
Relational Algebra in DBMS 2025 power point
AshokRachapalli1
 
CLOSURE OF AN ATTRIBUTE powerpontpresentatio
AshokRachapalli1
 
Relational algebra in database management system
AshokRachapalli1
 
DBMS-3.1 Normalization upto boyscodd normal form
AshokRachapalli1
 
Data base Users and Administrator pptx
AshokRachapalli1
 
Database Languages power point presentation
AshokRachapalli1
 
Relational Algebra in DBMS power ppoint pesenetation
AshokRachapalli1
 
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
ARRAYS in java with in details presentation.ppt
AshokRachapalli1
 
lecture-a-java-review .. this review ppt will help to the lectureres
AshokRachapalli1
 
17.INTRODUCTION TO SCHEMA REFINEMENT.pptx
AshokRachapalli1
 
joins in dbms its describes about how joins are important and necessity in d...
AshokRachapalli1
 
6.Database Languages lab-1.pptx
AshokRachapalli1
 
Chapter5 (1).ppt
AshokRachapalli1
 
Cache Memory.pptx
AshokRachapalli1
 
Addressing Modes.pptx
AshokRachapalli1
 
inputoutputorganization-140722085906-phpapp01.pptx
AshokRachapalli1
 
Ad

Recently uploaded (20)

PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Horarios de distribución de agua en julio
pegazohn1978
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Ad

Multi-Threading in Java power point presenetation

  • 2. • 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. • Java Multithreading is mostly used in games, animation, etc.
  • 3. Advantages of Java Multithreading • 1) It doesn't block the user because threads are independent and you can perform multiple operations at the 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 an exception occurs in a single thread.
  • 4. 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)
  • 5. 1) 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.
  • 6. 2) Thread-based Multitasking (Multithreading) • Threads share the same address space. • A thread is lightweight. • Cost of communication between the thread is low.
  • 7. What Is A Thread? • A thread is a lightweight unit of execution within a process. • It has its stack of memory and can run independently of other threads in the same process. • Threads share the same process resources, such as the heap and the code section. • The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
  • 8. • Every thread has a priority. • Threads with higher priority are executed in preference to threads with lower priority. • Each thread may or may not also be marked as a daemon. • When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread and is a daemon thread if and only if the creating thread is a daemon. • When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).
  • 9. Thread States And LifeCycle • NEW: A thread that has not yet started is in this state. • RUNNABLE: A thread executing in the Java virtual machine is in this state. • BLOCKED: A thread that is blocked waiting for a monitor lock is in this state. • WAITING: A thread that is waiting indefinitely for another thread to perform a particular action is in this state. • TIMED_WAITING: A thread that is waiting for another thread to act for up to a specified waiting time is in this state. • TERMINATED: A thread that has exited is in this state.
  • 10. Below is the thread lifecycle:
  • 11. • New (or Created): A thread is created by instantiating the Thread class. At this point, it is in the new state. • Thread myThread = new Thread(); • 2. Runnable (or Ready): After the thread is created, it moves to the runnable state when the start() method is called. The start() method internally calls the run() method, and the thread becomes ready for execution. • myThread.start(); // Moves the thread to the runnable state
  • 12. • Running: Once the scheduler selects the thread for execution, it enters the running state. The run() method contains the code that will be executed when the thread is running. • public void run() { // Code to be executed when the thread is running }
  • 13. • Blocked (or Waiting): A running thread may enter the blocked state if it encounters an operation that makes it wait, such as calling sleep(), wait(), or performing I/O operations. • // Example: Thread sleeps for 1 second try { Thread.sleep(1000); // Thread enters blocked state for 1 second } catch (InterruptedException e) { e.printStackTrace(); }
  • 14. • Terminated (or Dead): The thread enters the terminated state when the run() method completes its execution or when an uncaught exception occurs. • // Example: Completing the run method public void run() { // Code to be executed when the thread is running // ... // The thread terminates when this method completes }
  • 15. • It’s important to note that the start() method should be used to initiate the execution of a thread. The actual code to be executed should be placed in the run() method. Calling run() directly won’t create a new thread; it will execute the run() method in the context of the current thread. Using start() is essential for multithreading, as it signals the system to create a new thread and invoke the run() method in that new thread.