SlideShare a Scribd company logo
3
Most read
4
Most read
12
Most read
Asynchronous Programming
In Spring Framework
Presented By-:
Aayush Chimaniya
Contents-:
1. Introduction Of Asynchronous
2. Difference Between SYNC & MULTITHREADING & ASYNC
Programming.
3. Diagrammatic Differences
4. How Works Multithreading Programming & Asynchronous Programming
5. USE CASEs Of Asynchronous
6. Explore the asynchronous execution support in Spring Step-By-Step
7. Exception Handling In Asynchronous Programming
8. CONs Of Asynchronous Programming
9. Conclusion
INTRODUCTION OF ASYNCHRONOUS PROGRAMMING
1.Asynchronous programming is a form of parallel programming.
2. It allows a unit of work, to run separately from the primary thread. When the work
is complete, it notifies to the main thread.
3. It uses the same thread to process multiple requests without any request blocking the
thread.
4.Asynchronous programming has actually been around for a long time, but in recent
years, it’s become more widely used.
6.It helps to developers to
to run applications faster.
DIAGRAMMATIC DIFFERNECES
Seprate
Thread
DIFFERENCE BETWEEN SYNC & MULTITHREADING & ASYNC
PROGRAMMING
-Sync is single-thread, means one program will run at a time.
Sync is blocking pattern, means it can send one request to the server at a time
and will wait for, that request to be answered by the server.
-Multi-Thread is multi-thread programs, But every thread has assigned seprate task.
Multithreading programming is all about concurrent execution
of different functions.
-Async is multi-thread, but programs can run in parallel, if blocking comes
so it will create seprate thread and assigned the blocked task & continue it’s
execution.
Async is non-blocking, which means it will send multiple requests to a server.
In Multithreading Programming
-A thread is a single continuous flow of control within a program.
-Multithreading is a technique where the processor uses multiple threads to execute
multiple processes concurrently.
-In Multithreading every thread has different tasks like
( It’s like different workers has differnet jobs)
In Asynchronous Programming
-Asynchronous programming is about the asynchronous sequence of Tasks,
while multithreading is about multiple threads running in parallel
USE CASE Of Asynchronous
-Asynchronous programming should only be used in programming independent tasks.
1. Responsive UI is a great use case for asynchronous programming.
E.g. In a shopping app. When a user pulls up their order, the font size should
increase. Instead of waiting to load the history and update the font size, asynchronous
programming can make both actions simultaneously.
2. One more example is, It is use to make a call to an API.
where asynchronous functions say, “Get me the data from a website, and when it
gets here, insert that & fetched data back into my script.”
Explore the asynchronous execution support in Spring Step-By-Step
- Here We'll explore the asynchronous execution support in Spring
with the help of @Async & @EnableAsync etc.
- Simply put this annotation on a method of a bean with @Async
then it will make it execute in a separate thread.
OR
In other words, the caller will not wait for the completion of the called method.
Step:1 Enable Async Support
Let's start by enabling asynchronous processing with Java configuration.
-We'll do this by adding the @EnableAsync to a configuration class:
The enable annotation is enough to provide Async support.
By default , @EnableAsync detects Spring's @Async annotation
Or
The @EnableAsync annotation switches on Spring's ability to run @Async methods in a
background thread pool.
Step:2 Put @Async annotation on methods
Annotating a method of a bean with @Async will make it execute in a separate thread.
But We Have Follow Some Rules
Applying @Async Annotation On Types Of Methods
1. Methods With Void Return Type
(This is the simple way to configure a method with void return type
to run asynchronously)
2. Methods With Return Type (By Wrapping The Actual Return In The Future)
A. Methods With Void Return Type
-Here sendTextMessage is a seprate task so spring will create seprate thread
automatically because of @Async method.
B. Methods With Return Type
-Here sayHello is a seprate task so spring will create seprate thread
automatically, because of @Async method & return future object, untill
getting future object first thread will keep continuous searching for result
compilition.
Limitations On @Async annotation
1. It must be applied to public methods only.
2. Self-invocation not allowed.
(calling the async method from within the same class won’t work.)
Reasons-:
-The reasons are simple: The method needs to be public so that it can be proxied.
-And self-invocation doesn't work because it bypasses the proxy and calls the
underlying method directly like NORMAL METHOD.
Here @Async method will not work.
Exception Handling
-When a method return type is a Future, exception handling is easy. Coz
Future.get() method will throw the exception called ExecutionException.
-But if the return type of method is void, exceptions will not be propagated to the
caller thread. So, we need to add extra configurations to handle exceptions.
Extra configuration are like-:
1. First we have to create a custom async exception handler by
implementing AsyncUncaughtExceptionHandler interface.
& Override The handleUncaughtException() method,
which is invoked when any uncaught asynchronous exception will come.
2. After That we have to implement AsyncConfigurer interface in the configuration
class. & We also need to override
the getAsyncUncaughtExceptionHandler() method to return our custom
asynchronous exception handler.
Sample Code Of Exception Handling.
Configuration 1: Implementing AsyncUncaughtExceptionHandler to handle excep.
Configuration 2: Implementing AsyncConfigurer for returning Handler
CONs Of Asynchronous Programming
-It requires a lot of callbacks and recursive functions which might be typical to handle
during development.
-Asynchronous scripting might be difficult to implement in some programming
languages.
-Code can get messy and difficult to debug.
Conclusion
-In this session, we looked running asynchronous code with Spring boot.
-We started with the very basic configuration and annotation to make it work.
-But we also looked at more advanced configurations such as providing our own
executor or exception handling strategies, etc…
Thank You!

More Related Content

PPTX
Asynchronous and parallel programming
Anil Kumar
 
PPTX
Async programming in c#
Ahasanul Kalam Akib
 
PDF
Asynchronous programming with Java & Spring
Ravindra Ranwala
 
PDF
Async.pdf
AhmedMaherAlmaqtari
 
PPTX
Synchronous vs Asynchronous Programming
jeetendra mandal
 
PDF
Programming Sideways: Asynchronous Techniques for Android
Emanuele Di Saverio
 
PPTX
Essential API Facade Patterns: Synchronous to Asynchronous Conversion (Episod...
Apigee | Google Cloud
 
PDF
JavaCro'15 - Spring @Async - Dragan Juričić
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Asynchronous and parallel programming
Anil Kumar
 
Async programming in c#
Ahasanul Kalam Akib
 
Asynchronous programming with Java & Spring
Ravindra Ranwala
 
Synchronous vs Asynchronous Programming
jeetendra mandal
 
Programming Sideways: Asynchronous Techniques for Android
Emanuele Di Saverio
 
Essential API Facade Patterns: Synchronous to Asynchronous Conversion (Episod...
Apigee | Google Cloud
 

Similar to Asynchronous Programming.pptx (20)

PDF
A first look into the Project Loom in Java
Lukas Steinbrecher
 
PPTX
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
PROIDEA
 
PPTX
Asynchronous programming using CompletableFutures in Java
Oresztész Margaritisz
 
DOCX
Asynchronyin net
Soacat Blogspot
 
KEY
Asynchronous Programming
LearnNowOnline
 
PDF
Async Await for Mobile Apps
Craig Dunn
 
PPTX
Concurrency in c#
RezaHamidpour
 
PDF
Asynchronous Programming. Talk from ESUG2024
ESUG
 
PDF
Asynchronous API in Java8, how to use CompletableFuture
José Paumard
 
PDF
Asynchronous Programming in C# and .NET Core - Edukite
EduKite
 
PDF
Speedup Your Code Through Asynchronous Programing
ScyllaDB
 
PPTX
AsynchronousProgrammingDesignPatterns.pptx
Abhishek Sagar
 
PDF
Session 9 Android Web Services - Part 2.pdf
EngmohammedAlzared
 
PPTX
History of asynchronous in .NET
Marcin Tyborowski
 
PPTX
Async CTP 3 Presentation for MUGH 2012
Sri Kanth
 
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
PDF
Loom and concurrency latest
Srinivasan Raghavan
 
PDF
Async programming in Python_ Build non-blocking, scalable apps with coroutine...
Peerbits
 
PDF
The art of concurrent programming
Iskren Chernev
 
PDF
F#语言对异步程序设计的支持
jeffz
 
A first look into the Project Loom in Java
Lukas Steinbrecher
 
4Developers 2015: Programowanie synchroniczne i asynchroniczne - dwa światy k...
PROIDEA
 
Asynchronous programming using CompletableFutures in Java
Oresztész Margaritisz
 
Asynchronyin net
Soacat Blogspot
 
Asynchronous Programming
LearnNowOnline
 
Async Await for Mobile Apps
Craig Dunn
 
Concurrency in c#
RezaHamidpour
 
Asynchronous Programming. Talk from ESUG2024
ESUG
 
Asynchronous API in Java8, how to use CompletableFuture
José Paumard
 
Asynchronous Programming in C# and .NET Core - Edukite
EduKite
 
Speedup Your Code Through Asynchronous Programing
ScyllaDB
 
AsynchronousProgrammingDesignPatterns.pptx
Abhishek Sagar
 
Session 9 Android Web Services - Part 2.pdf
EngmohammedAlzared
 
History of asynchronous in .NET
Marcin Tyborowski
 
Async CTP 3 Presentation for MUGH 2012
Sri Kanth
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
Loom and concurrency latest
Srinivasan Raghavan
 
Async programming in Python_ Build non-blocking, scalable apps with coroutine...
Peerbits
 
The art of concurrent programming
Iskren Chernev
 
F#语言对异步程序设计的支持
jeffz
 
Ad

More from Aayush Chimaniya (6)

PPTX
Jasper Reports.pptx
Aayush Chimaniya
 
PPTX
Microservices Api Gateway Eureka Server.pptx
Aayush Chimaniya
 
PPTX
MongoDB.pptx
Aayush Chimaniya
 
PPT
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
PDF
SADBlood Bank.pdf
Aayush Chimaniya
 
PDF
Electric Vehicle
Aayush Chimaniya
 
Jasper Reports.pptx
Aayush Chimaniya
 
Microservices Api Gateway Eureka Server.pptx
Aayush Chimaniya
 
MongoDB.pptx
Aayush Chimaniya
 
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
SADBlood Bank.pdf
Aayush Chimaniya
 
Electric Vehicle
Aayush Chimaniya
 
Ad

Recently uploaded (20)

PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 

Asynchronous Programming.pptx

  • 1. Asynchronous Programming In Spring Framework Presented By-: Aayush Chimaniya
  • 2. Contents-: 1. Introduction Of Asynchronous 2. Difference Between SYNC & MULTITHREADING & ASYNC Programming. 3. Diagrammatic Differences 4. How Works Multithreading Programming & Asynchronous Programming 5. USE CASEs Of Asynchronous 6. Explore the asynchronous execution support in Spring Step-By-Step 7. Exception Handling In Asynchronous Programming 8. CONs Of Asynchronous Programming 9. Conclusion
  • 3. INTRODUCTION OF ASYNCHRONOUS PROGRAMMING 1.Asynchronous programming is a form of parallel programming. 2. It allows a unit of work, to run separately from the primary thread. When the work is complete, it notifies to the main thread. 3. It uses the same thread to process multiple requests without any request blocking the thread. 4.Asynchronous programming has actually been around for a long time, but in recent years, it’s become more widely used. 6.It helps to developers to to run applications faster.
  • 5. DIFFERENCE BETWEEN SYNC & MULTITHREADING & ASYNC PROGRAMMING -Sync is single-thread, means one program will run at a time. Sync is blocking pattern, means it can send one request to the server at a time and will wait for, that request to be answered by the server. -Multi-Thread is multi-thread programs, But every thread has assigned seprate task. Multithreading programming is all about concurrent execution of different functions. -Async is multi-thread, but programs can run in parallel, if blocking comes so it will create seprate thread and assigned the blocked task & continue it’s execution. Async is non-blocking, which means it will send multiple requests to a server.
  • 6. In Multithreading Programming -A thread is a single continuous flow of control within a program. -Multithreading is a technique where the processor uses multiple threads to execute multiple processes concurrently. -In Multithreading every thread has different tasks like ( It’s like different workers has differnet jobs) In Asynchronous Programming -Asynchronous programming is about the asynchronous sequence of Tasks, while multithreading is about multiple threads running in parallel
  • 7. USE CASE Of Asynchronous -Asynchronous programming should only be used in programming independent tasks. 1. Responsive UI is a great use case for asynchronous programming. E.g. In a shopping app. When a user pulls up their order, the font size should increase. Instead of waiting to load the history and update the font size, asynchronous programming can make both actions simultaneously. 2. One more example is, It is use to make a call to an API. where asynchronous functions say, “Get me the data from a website, and when it gets here, insert that & fetched data back into my script.”
  • 8. Explore the asynchronous execution support in Spring Step-By-Step - Here We'll explore the asynchronous execution support in Spring with the help of @Async & @EnableAsync etc. - Simply put this annotation on a method of a bean with @Async then it will make it execute in a separate thread. OR In other words, the caller will not wait for the completion of the called method.
  • 9. Step:1 Enable Async Support Let's start by enabling asynchronous processing with Java configuration. -We'll do this by adding the @EnableAsync to a configuration class: The enable annotation is enough to provide Async support. By default , @EnableAsync detects Spring's @Async annotation Or The @EnableAsync annotation switches on Spring's ability to run @Async methods in a background thread pool.
  • 10. Step:2 Put @Async annotation on methods Annotating a method of a bean with @Async will make it execute in a separate thread. But We Have Follow Some Rules Applying @Async Annotation On Types Of Methods 1. Methods With Void Return Type (This is the simple way to configure a method with void return type to run asynchronously) 2. Methods With Return Type (By Wrapping The Actual Return In The Future)
  • 11. A. Methods With Void Return Type -Here sendTextMessage is a seprate task so spring will create seprate thread automatically because of @Async method.
  • 12. B. Methods With Return Type -Here sayHello is a seprate task so spring will create seprate thread automatically, because of @Async method & return future object, untill getting future object first thread will keep continuous searching for result compilition.
  • 13. Limitations On @Async annotation 1. It must be applied to public methods only. 2. Self-invocation not allowed. (calling the async method from within the same class won’t work.) Reasons-: -The reasons are simple: The method needs to be public so that it can be proxied. -And self-invocation doesn't work because it bypasses the proxy and calls the underlying method directly like NORMAL METHOD. Here @Async method will not work.
  • 14. Exception Handling -When a method return type is a Future, exception handling is easy. Coz Future.get() method will throw the exception called ExecutionException. -But if the return type of method is void, exceptions will not be propagated to the caller thread. So, we need to add extra configurations to handle exceptions. Extra configuration are like-: 1. First we have to create a custom async exception handler by implementing AsyncUncaughtExceptionHandler interface. & Override The handleUncaughtException() method, which is invoked when any uncaught asynchronous exception will come. 2. After That we have to implement AsyncConfigurer interface in the configuration class. & We also need to override the getAsyncUncaughtExceptionHandler() method to return our custom asynchronous exception handler.
  • 15. Sample Code Of Exception Handling. Configuration 1: Implementing AsyncUncaughtExceptionHandler to handle excep. Configuration 2: Implementing AsyncConfigurer for returning Handler
  • 16. CONs Of Asynchronous Programming -It requires a lot of callbacks and recursive functions which might be typical to handle during development. -Asynchronous scripting might be difficult to implement in some programming languages. -Code can get messy and difficult to debug.
  • 17. Conclusion -In this session, we looked running asynchronous code with Spring boot. -We started with the very basic configuration and annotation to make it work. -But we also looked at more advanced configurations such as providing our own executor or exception handling strategies, etc…