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
Training – Going Async
PPTX
Task parallel library presentation
PDF
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
PPTX
Async in .NET
PPSX
Async-await best practices in 10 minutes
PDF
Multi t hreading_14_10
DOCX
Asynchronyin net
PDF
Async Await for Mobile Apps
Training – Going Async
Task parallel library presentation
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
Async in .NET
Async-await best practices in 10 minutes
Multi t hreading_14_10
Asynchronyin net
Async Await for Mobile Apps

Similar to Asynchronous Programming.pptx (20)

PPTX
C# Async Await
PDF
Android development training programme , Day 3
PPTX
End to-end async and await
PDF
Concurrency and parallel in .net
ODT
Designing Better API
ODP
Drilling the Async Library
PDF
Unit2-Part2-MultithreadAlgos.pptx.pdf
PPTX
Lecture 23-24.pptx
PPTX
Asynchronous Programming in .NET
PDF
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
KEY
Capistrano, Puppet, and Chef
PPTX
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
PPTX
Asynchronous programming - .NET Way
PPTX
Asynchronous programming in ASP.NET
PPTX
Ddd melbourne 2011 C# async ctp
DOC
An investigation into Cluster CPU load balancing in the JVM
PDF
PyCon Canada 2019 - Introduction to Asynchronous Programming
PDF
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
PPTX
Async programming in c#
PDF
Introduction to Python Asyncio
C# Async Await
Android development training programme , Day 3
End to-end async and await
Concurrency and parallel in .net
Designing Better API
Drilling the Async Library
Unit2-Part2-MultithreadAlgos.pptx.pdf
Lecture 23-24.pptx
Asynchronous Programming in .NET
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
Capistrano, Puppet, and Chef
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
Asynchronous programming - .NET Way
Asynchronous programming in ASP.NET
Ddd melbourne 2011 C# async ctp
An investigation into Cluster CPU load balancing in the JVM
PyCon Canada 2019 - Introduction to Asynchronous Programming
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
Async programming in c#
Introduction to Python Asyncio
Ad

More from Aayush Chimaniya (6)

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

Recently uploaded (20)

PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
HVAC Specification 2024 according to central public works department
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Trump Administration's workforce development strategy
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction to Building Materials
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
HVAC Specification 2024 according to central public works department
Chinmaya Tiranga quiz Grand Finale.pdf
Computing-Curriculum for Schools in Ghana
Virtual and Augmented Reality in Current Scenario
AI-driven educational solutions for real-life interventions in the Philippine...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Trump Administration's workforce development strategy
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction to Building Materials
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
What if we spent less time fighting change, and more time building what’s rig...
LDMMIA Reiki Yoga Finals Review Spring Summer
Chinmaya Tiranga Azadi Quiz (Class 7-8 )

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…