SlideShare a Scribd company logo
1 of 24
Presentation on:
MULTI-THREADING AND
THREAD SYNCHRONIZATION
SUBMITTED BY: Juhi Kumari
Multithreading in java is  a  process  of  executing 
multiple threads simultaneously.
Thread  is  basically  a  lightweight  sub-process,  a 
smallest  unit  of  processing.  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.
Multithreading in Java
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.
What is Thread in java
A  thread  is  a  lightweight  sub  process,  a 
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  shares  a  common  memory 
area.
Life cycle of a Thread (Thread States):-
 A thread can be in one of the five states. According
to sun, there is only 4 states in thread life cycle in
java  new,runnable,non-runnable and terminated.
There is no running state. But for better
understanding the threads, we are explaining it in
the 5 states.
 The life cycle of the thread in java is controlled by
JVM.
New :
When a thread is created by new statement but not yet
run, it is in newborn state.
It is also referred to as a born thread.
In this state, the local data members are allocated and
initialized.
Runnable :
 The runnable state means that a thread is ready to run and
is waiting for the availability of the processor.
In other words, threads are in this state in a queue and
wait their turns to be execution.
RUNNING STATE:-
 Running means that the thread has control on the
processor.
 The thread is in running state if the thread
scheduler has selected it.
 System assigns processor to thread (thread begins
executing).
 When run completes or terminates, enters dead
state.
Non-Runnable (Blocked):-
This is the state when the thread is still alive, but is
currently not eligible to run.
 A thread can enter in this state because of waiting the
resources that are hold by another thread.
Terminated:-
A thread can be considered dead when its run() method
completes.
If any thread comes on this state that means it cannot
ever run again.
SYNCHRONIZATION
 Synchronization is the capability to control the
access of multiple threads to any shared
resource.
 It is a better option if we want to allow only
one thread to access a shared resource at a
particular time.
 It is mainly use to prevent thread interference.
Synchronization can be implemented in
two ways in Java:
 Mutual Exclusion.
 Inter- Thread Communication(Cooperation)
MUTUAL EXCLUSION
Mutual Exclusive helps keep threads from interfering with one
another while sharing data. This can be done by three ways in
java:
by synchronized method
by synchronized block
by static synchronization
JAVA SYNCHRONIZED METHOD
 If you declare any method as synchronized, it is known as
synchronized method.
 Synchronized method is used to lock an object for any shared
resource.
 When a thread invokes a synchronized method, it automatically
acquires the lock for that object and releases it when the thread
completes its task.
Syntax:
Synchronized return_type method_name()
{//block}
JAVA SYNCHRONIZED BLOCK
 Synchronized block can be used to perform synchronization on any
specific resource of the method.
 Suppose you have 50 lines of code in your method, but you want to
synchronize only 5 lines, you can use synchronized block.
 If you put all the codes of the method in the synchronized block, it
will work same as the synchronized method.
 Points to remember for Synchronized block
 Synchronized block is used to lock an object for any shared
resource.
 Scope of synchronized block is smaller than the method.
Syntax to use synchronized block
synchronized (object reference expression)
 {   
  //code block   
}  
STATIC SYNCHRONIZATION
 If you make any static method as synchronized, the lock
will be on the class not on object.
class Table{  
 synchronized void printTable(int n)
{//synchronized method  
   for(int i=1;i<=5;i++){  
     System.out.println(n*i);  
     try{  
      Thread.sleep(400);  
     }catch(Exception e)
        {System.out.println(e);}  
   }   
 }  
}  
class MyThread1 extends Thread{  
Table t;  
MyThread1(Table t){  
this.t=t;  
}  
public void run(){  
t.printTable(5);  
}  
  
}  
class MyThread2 extends Thread{  
Table t;  
MyThread2(Table t){  
this.t=t;  
}  
public void run(){  
t.printTable(100);  
}  
}  
  
public class TestSynchronization2{  
public static void main(String args[]){  
Table obj = new Table();//only one object  
MyThread1 t1=new MyThread1(obj);  
MyThread2 t2=new MyThread2(obj);  
t1.start();  
t2.start();  
}  
}  
Output:
5
10
15
20
25
100
200
300
400
500
INTER-THREAD
COMMUNICATION(COOPERATION)
 Cooperation (Inter-thread communication) is a mechanism
in which a thread is paused running in its critical section
and another thread is allowed to enter (or lock) in the
same critical section to be executed.It is implemented by
following methods of Object class:
 wait()
 notify()
 notifyAll()
1) wait() method
 Causes current thread to release the lock and wait until
either another thread invokes the notify() method or the
notifyAll() method for this object, or a specified amount
of time has elapsed.
 The current thread must own this object's monitor, so it
must be called from the synchronized method only
otherwise it will throw exception.
public final void wait()throws InterruptedException
2) notify() method
 Wakes up a single thread that is waiting on this object's
monitor. If any threads are waiting on this object, one of
them is chosen to be awakened. The choice is arbitrary
and occurs at the discretion of the implementation.
Syntax:
public final void notify()
3) notifyAll() method
 Wakes up all threads that are waiting on this object's
monitor.
Syntax:
public final void notifyAll()
Thread

More Related Content

What's hot

The Anterior component of occlusal force.pptx
The Anterior component of occlusal force.pptxThe Anterior component of occlusal force.pptx
The Anterior component of occlusal force.pptx
dhanvi31
 

What's hot (20)

Postero anterior cephalometrics /certified fixed orthodontic courses by Indi...
Postero anterior  cephalometrics /certified fixed orthodontic courses by Indi...Postero anterior  cephalometrics /certified fixed orthodontic courses by Indi...
Postero anterior cephalometrics /certified fixed orthodontic courses by Indi...
 
ceph & model Mock surgery /certified fixed orthodontic courses by Indian dent...
ceph & model Mock surgery /certified fixed orthodontic courses by Indian dent...ceph & model Mock surgery /certified fixed orthodontic courses by Indian dent...
ceph & model Mock surgery /certified fixed orthodontic courses by Indian dent...
 
Class 3 malocclusion
Class 3 malocclusionClass 3 malocclusion
Class 3 malocclusion
 
Soft tissue cephalometrics analysis /certified fixed orthodontic courses by I...
Soft tissue cephalometrics analysis /certified fixed orthodontic courses by I...Soft tissue cephalometrics analysis /certified fixed orthodontic courses by I...
Soft tissue cephalometrics analysis /certified fixed orthodontic courses by I...
 
The Anterior component of occlusal force.pptx
The Anterior component of occlusal force.pptxThe Anterior component of occlusal force.pptx
The Anterior component of occlusal force.pptx
 
Role of cephalometry in orthdodontics /certified fixed orthodontic courses by...
Role of cephalometry in orthdodontics /certified fixed orthodontic courses by...Role of cephalometry in orthdodontics /certified fixed orthodontic courses by...
Role of cephalometry in orthdodontics /certified fixed orthodontic courses by...
 
Facebow 1
Facebow 1Facebow 1
Facebow 1
 
Is lag screw fixation superior to plate fixation to treat fractures of the ma...
Is lag screw fixation superior to plate fixation to treat fractures of the ma...Is lag screw fixation superior to plate fixation to treat fractures of the ma...
Is lag screw fixation superior to plate fixation to treat fractures of the ma...
 
Elastomeric and coil springs
Elastomeric and coil springsElastomeric and coil springs
Elastomeric and coil springs
 
Development of mandible /certified fixed orthodontic courses by Indian dental...
Development of mandible /certified fixed orthodontic courses by Indian dental...Development of mandible /certified fixed orthodontic courses by Indian dental...
Development of mandible /certified fixed orthodontic courses by Indian dental...
 
Molar intrusion
Molar intrusionMolar intrusion
Molar intrusion
 
Management of deviated midline
Management of deviated midlineManagement of deviated midline
Management of deviated midline
 
Growth prediction and control
Growth prediction and controlGrowth prediction and control
Growth prediction and control
 
WICK ALEXANDER TECNIQUE OF PRE-ADJUSTED EDGEWISE APPLIANCE
WICK ALEXANDER TECNIQUE OF PRE-ADJUSTED EDGEWISE APPLIANCEWICK ALEXANDER TECNIQUE OF PRE-ADJUSTED EDGEWISE APPLIANCE
WICK ALEXANDER TECNIQUE OF PRE-ADJUSTED EDGEWISE APPLIANCE
 
Twin block
Twin block Twin block
Twin block
 
Robotics in orthodontics
Robotics in orthodontics Robotics in orthodontics
Robotics in orthodontics
 
6 occlusion
6 occlusion6 occlusion
6 occlusion
 
3 d cephalometrics and morphometrics /certified fixed orthodontic courses
3 d cephalometrics and morphometrics   /certified fixed orthodontic courses   3 d cephalometrics and morphometrics   /certified fixed orthodontic courses
3 d cephalometrics and morphometrics /certified fixed orthodontic courses
 
Classification of malocclusion /certified fixed orthodontic courses by India...
Classification of malocclusion  /certified fixed orthodontic courses by India...Classification of malocclusion  /certified fixed orthodontic courses by India...
Classification of malocclusion /certified fixed orthodontic courses by India...
 
The inman aligner
The inman alignerThe inman aligner
The inman aligner
 

Similar to Thread

07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Multithreading
MultithreadingMultithreading
Multithreading
sagsharma
 

Similar to Thread (20)

07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter Kit
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java
JavaJava
Java
 
multithreading
multithreadingmultithreading
multithreading
 
Java
JavaJava
Java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
 

Recently uploaded

ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for vote
RaunakRastogi4
 
Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cherry
 
COMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCOMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demerits
Cherry
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
Cherry
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
Cherry
 
Lipids: types, structure and important functions.
Lipids: types, structure and important functions.Lipids: types, structure and important functions.
Lipids: types, structure and important functions.
Cherry
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
MohamedFarag457087
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptx
seri bangash
 

Recently uploaded (20)

GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
 
ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for vote
 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
 
Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.
 
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and SpectrometryFAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
 
Energy is the beat of life irrespective of the domains. ATP- the energy curre...
Energy is the beat of life irrespective of the domains. ATP- the energy curre...Energy is the beat of life irrespective of the domains. ATP- the energy curre...
Energy is the beat of life irrespective of the domains. ATP- the energy curre...
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptx
 
COMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCOMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demerits
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization pptx
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
 
Lipids: types, structure and important functions.
Lipids: types, structure and important functions.Lipids: types, structure and important functions.
Lipids: types, structure and important functions.
 
Use of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxUse of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptx
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
 
GBSN - Microbiology (Unit 5) Concept of isolation
GBSN - Microbiology (Unit 5) Concept of isolationGBSN - Microbiology (Unit 5) Concept of isolation
GBSN - Microbiology (Unit 5) Concept of isolation
 
Taphonomy and Quality of the Fossil Record
Taphonomy and Quality of the  Fossil RecordTaphonomy and Quality of the  Fossil Record
Taphonomy and Quality of the Fossil Record
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.
 
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspects
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptx
 

Thread