際際滷

際際滷Share a Scribd company logo
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
objects 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 objects
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 (20)

Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
Data types
Data typesData types
Data types
myrajendra
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
JVM
JVMJVM
JVM
baabtra.com - No. 1 supplier of quality freshers
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
Strings
StringsStrings
Strings
naslin prestilda
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha

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
keval_thummar
unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
Multi threading
Multi threadingMulti threading
Multi threading
gndu
22 multi threading iv
22 multi threading iv22 multi threading iv
22 multi threading iv
Ravindra Rathore
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual Project
ThienSi Le
multithreading.pptx
multithreading.pptxmultithreading.pptx
multithreading.pptx
Sravanibitragunta
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronization
priyabogra1
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
Jayant Dalvi
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
Tuan Chau
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
Deivaa
Java Thread
Java ThreadJava Thread
Java Thread
DeeptiJava
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
HemantSharma134028
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interface
keval_thummar
unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
Multi threading
Multi threadingMulti threading
Multi threading
gndu
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual Project
ThienSi Le
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronization
priyabogra1
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
Jayant Dalvi
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
Tuan Chau
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
Deivaa
Java Thread
Java ThreadJava Thread
Java Thread
DeeptiJava
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat

Recently uploaded (20)

Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
How to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of SaleHow to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of Sale
Celine George
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
Storytelling instructions...............
Storytelling instructions...............Storytelling instructions...............
Storytelling instructions...............
Alexander Benito
Useful environment methods in Odoo 18 - Odoo 際際滷s
Useful environment methods in Odoo 18 - Odoo 際際滷sUseful environment methods in Odoo 18 - Odoo 際際滷s
Useful environment methods in Odoo 18 - Odoo 際際滷s
Celine George
Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Ajaz Hussain
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Database population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slidesDatabase population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slides
Celine George
Modeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptxModeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptx
maribethlacno2
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
A PPT Presentation on The Princess and the God: A tale of ancient India  by A...A PPT Presentation on The Princess and the God: A tale of ancient India  by A...
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
Beena E S
How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18
Celine George
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdfBlind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Ajaz Hussain
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷sHow to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
Celine George
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
How to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of SaleHow to Configure Restaurants in Odoo 17 Point of Sale
How to Configure Restaurants in Odoo 17 Point of Sale
Celine George
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1
QuickBooks Desktop to QuickBooks Online How to Make the Move
QuickBooks Desktop to QuickBooks Online  How to Make the MoveQuickBooks Desktop to QuickBooks Online  How to Make the Move
QuickBooks Desktop to QuickBooks Online How to Make the Move
TechSoup
Storytelling instructions...............
Storytelling instructions...............Storytelling instructions...............
Storytelling instructions...............
Alexander Benito
Useful environment methods in Odoo 18 - Odoo 際際滷s
Useful environment methods in Odoo 18 - Odoo 際際滷sUseful environment methods in Odoo 18 - Odoo 際際滷s
Useful environment methods in Odoo 18 - Odoo 際際滷s
Celine George
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Blind Spots in AI and Formulation Science Knowledge Pyramid (Updated Perspect...
Ajaz Hussain
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptxTLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
TLE 7 - 2nd Topic - Codes and Standards in Industrial Arts Services.pptx
RizaBedayo
Database population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slidesDatabase population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slides
Celine George
Modeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptxModeling-Simple-Equation-Using-Bar-Models.pptx
Modeling-Simple-Equation-Using-Bar-Models.pptx
maribethlacno2
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
A PPT Presentation on The Princess and the God: A tale of ancient India  by A...A PPT Presentation on The Princess and the God: A tale of ancient India  by A...
A PPT Presentation on The Princess and the God: A tale of ancient India by A...
Beena E S
How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18How to Modify Existing Web Pages in Odoo 18
How to Modify Existing Web Pages in Odoo 18
Celine George
TPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategyTPR Data strategy 2025 (1).pdf Data strategy
TPR Data strategy 2025 (1).pdf Data strategy
Henry Tapper
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdfBlind spots in AI and Formulation Science, IFPAC 2025.pdf
Blind spots in AI and Formulation Science, IFPAC 2025.pdf
Ajaz Hussain
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷sHow to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
Celine George

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 objects 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 objects 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