2. 2
What is asynchronous programming?
• Asynchronous means that the current thread is freed while you are waiting for a response to
some I/O operation. (local storage, a network request, etc.)
• A sort of parallel programming that permits a unit of labor to run separately from the first
application thread.
• Is a key technique that makes it straight forward to handle blocking I/O and concurrent
operations on multiple cores.
• Asynchronous code isn't about multi-threading. Actually the opposite: Part of the benefit of
asynchronous code is to not need more threads.
3. 3
What is asynchronous programming?
There are two types of work that are done asynchronously:
I/O-bound operations could be
• File-system accesses
• HTTP requests,
• API calls, or database queries.
CPU-bound operations would be
• like encrypting data
• complex calculations
• image or document management.
Which are:
• Heavy computational work
• Needs continuous CPU involvement
Hence,
This will need a dedicated thread, and
will generally use a ThreadPool thread
• Which can be done without CPU
system.
• May not need any dedicated threads
• The network or disk driver may handle
it by themselves
4. 4
Benefits?
• Keep the UI of our app responsive.
• Can improve the performance of our application.
• Utilization of multi-core systems
• Avoid thread pool starvation
5. 5
Synchronous vs Asynchronous
Asynchronous
In asynchronous operations, on the other hand,
you can move to another task before the previous
one finishes. This way, with asynchronous
programming you’re able to deal with multiple
requests simultaneously,
Synchronous
In synchronous operations tasks are
performed one at a time and only when one
is completed, the following is unblocked. In
other words, you need to wait for a task to
finish to move to the next one.
11. 11
Async await in practice
Syntax:
public async void/Task/Task<T> MethodAsync(param1, param2)
{
doSynchronousTask();
await doAsyncTask();
}
• The async enables the await functionality in the method
• You can not use await without using the async
• A method can be declared as async without using await in the method body. It
does work, but the just runs synchronously
• Three types of return type void/Task/Task<T>
12. 12
Async await in practice
Blocking Code
• GetAwaiter().GetResult()
• Result
• Wait
Non-Blocking Code
• await
13. 13
How is it useful?
For example, consider a web API call that reads data from a database. What happens when 1000 requests come in at
the same time?
Synchronous Way:
• Need a separate thread for each request
• ASP.NET has a maximum thread count (ex. 3000)
• Max count will be reached very soon
• Have to wait until some of the first requests are completed before they can even start
Asynchronous Way:
• As soon as the database request is made, the thread is freed while it waits for a response from the database.
• During that waiting time, ASP.NET can use that thread to start processing a new request.
• The result is that you need less threads to do the same amount of work
• Increase the scalability
14. 14
Drawbacks
• Code gets more complex and harder to maintain.
• There is increased memory allocation, as some objects have to stay alive longer while
awaiting other code to be executed.
• It can get hard to find bugs occurring in asynchronous tasks.
• When we're writing an asynchronous piece of code, all our application code tends to
become asynchronous.