SlideShare a Scribd company logo
1 of 12
SNS COLLEGE OF TECHNOLOGY
(An Autonomous Institutions)
19CST102 - OBJECT ORIENTED PROGRAMMING
I YEAR / II SEMESTER
UNIT IV – MULTITHREADING IN JAVA
TOPIC : INTER THREAD COMMUNICATION
Presented by:
Vishnnu K.S
(713522CS176)
Vilashini.V
(713522CS172)
Guided by :
Mr.Selvakumar. N
AP/CSE
Java Threads
• Threads allows a program to operate more efficiently by doing
multiple things at the same time.
• Threads can be used to perform complicated tasks in the
background without interrupting the main program.
Inter-thread Communication in Java
• Inter thread communication is all about allowing threads to
communicate with each other.
• It is implemented by following methods of Object class:
• Wait()
• Notify()
• NotifyAll()
Wait() method
• The 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 objects monitor,so it must be
called from the synchronized method only otherwise it will throw
exception.
Notify() method
• The 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 choosen to be
awakened.
Syntax:
Public final void notify()
NotifyAll() method
• Wakes up all threads that are waiting on this object’s
monitor.
Syntax:
Public final void notifyAll()
Understanding the process of inter-thread communication
• The point to point explanation of the above diagram is as follows:
• Threads enter to acquire lock.
• Lock is acquired by on thread.
• Now thread goes to waiting state if you call wait() method on the object.
Otherwise it releases the lock and exits.
• If you call notify() or notify All() method, thread moves to the notified state
(runnable state).
• Now thread is available to acquire lock.
• After completion of the task, thread releases the lock and exits the monitor
state of the object.
Understanding the process of inter-thread
communication
Difference between wait and sleep
wait() sleep()
The wait() method releases the
lock.
The sleep() method doesn't release
the lock.
It is a method of Object class It is a method of Thread class
It is the non-static method It is the static method
It should be notified by notify() or
notifyAll() methods
After the specified amount of time,
sleep is completed.
Program
Class Customer
{
Int amount=10000;
Synchronized void withdraw(int amount){
System.out.println(“going to withdraw….”);
If(this.amount<amount){
System.out.println(“less balance;Waiting for
deposit….”);
}
This.amount-=amount;
System.out.println(“withdraw completed….”);
}
Synchronized void deposit(int amount){
System.out.println(“going to deposit….”);
This.amount+=amount;
System.out.println(“deposit completed…”);
Notify();
}
}
Class Test{
Public staic void main(String args[]){
New Thread(){
Public void run(){c.withdraw(15000);}
}.start();
New Thread(){
Public void run(){c.deposit(10000);}
}.start();
}}
OUTPUT
going to withdraw….
less balance;waiting for deposit…..
going to deposit…..
deposit completed….
withdraw completed
Thank you

More Related Content

What's hot

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentationManav Prasad
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In JavaRajan Shah
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and ConcurrencySunil OS
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
Finalize() method
Finalize() methodFinalize() method
Finalize() methodJadavsejal
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
JAVA ENVIRONMENT
JAVA  ENVIRONMENTJAVA  ENVIRONMENT
JAVA ENVIRONMENTjosemachoco
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 

What's hot (20)

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
JAVA ENVIRONMENT
JAVA  ENVIRONMENTJAVA  ENVIRONMENT
JAVA ENVIRONMENT
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 

Similar to Inter Thread Communicationn.pptx

Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfacekeval_thummar
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptxnimbalkarvikram966
 
CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual ProjectThienSi Le
 
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronizationpriyabogra1
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptxramyan49
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in JavaJayant Dalvi
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreadingTuan Chau
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptxHKShab
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharpDeivaa
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptxTREXSHyNE
 

Similar to Inter Thread Communicationn.pptx (20)

Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interface
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Multi threading
Multi threadingMulti threading
Multi threading
 
22 multi threading iv
22 multi threading iv22 multi threading iv
22 multi threading iv
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual Project
 
multithreading.pptx
multithreading.pptxmultithreading.pptx
multithreading.pptx
 
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronization
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
 
Java Thread
Java ThreadJava Thread
Java Thread
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Java threading
Java threadingJava threading
Java threading
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
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

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Recently uploaded (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Inter Thread Communicationn.pptx

  • 1. SNS COLLEGE OF TECHNOLOGY (An Autonomous Institutions) 19CST102 - OBJECT ORIENTED PROGRAMMING I YEAR / II SEMESTER UNIT IV – MULTITHREADING IN JAVA TOPIC : INTER THREAD COMMUNICATION Presented by: Vishnnu K.S (713522CS176) Vilashini.V (713522CS172) Guided by : Mr.Selvakumar. N AP/CSE
  • 2. Java Threads • Threads allows a program to operate more efficiently by doing multiple things at the same time. • Threads can be used to perform complicated tasks in the background without interrupting the main program.
  • 3. Inter-thread Communication in Java • Inter thread communication is all about allowing threads to communicate with each other. • It is implemented by following methods of Object class: • Wait() • Notify() • NotifyAll()
  • 4. Wait() method • The 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 objects monitor,so it must be called from the synchronized method only otherwise it will throw exception.
  • 5. Notify() method • The 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 choosen to be awakened. Syntax: Public final void notify()
  • 6. NotifyAll() method • Wakes up all threads that are waiting on this object’s monitor. Syntax: Public final void notifyAll()
  • 7. Understanding the process of inter-thread communication
  • 8. • The point to point explanation of the above diagram is as follows: • Threads enter to acquire lock. • Lock is acquired by on thread. • Now thread goes to waiting state if you call wait() method on the object. Otherwise it releases the lock and exits. • If you call notify() or notify All() method, thread moves to the notified state (runnable state). • Now thread is available to acquire lock. • After completion of the task, thread releases the lock and exits the monitor state of the object. Understanding the process of inter-thread communication
  • 9. Difference between wait and sleep wait() sleep() The wait() method releases the lock. The sleep() method doesn't release the lock. It is a method of Object class It is a method of Thread class It is the non-static method It is the static method It should be notified by notify() or notifyAll() methods After the specified amount of time, sleep is completed.
  • 10. Program Class Customer { Int amount=10000; Synchronized void withdraw(int amount){ System.out.println(“going to withdraw….”); If(this.amount<amount){ System.out.println(“less balance;Waiting for deposit….”); } This.amount-=amount; System.out.println(“withdraw completed….”); } Synchronized void deposit(int amount){ System.out.println(“going to deposit….”); This.amount+=amount; System.out.println(“deposit completed…”); Notify(); } } Class Test{ Public staic void main(String args[]){ New Thread(){ Public void run(){c.withdraw(15000);} }.start(); New Thread(){ Public void run(){c.deposit(10000);} }.start(); }}
  • 11. OUTPUT going to withdraw…. less balance;waiting for deposit….. going to deposit….. deposit completed…. withdraw completed