Synchronous Programming model – In this programming model, A thread is assigned to one task and starts working on it. Once the task completes then it is available for the next task. In this model, it cannot leave the executing task in mid to take up another task. Let’s discuss how this model works in single and multi-threaded environments.
Single Threaded – If we have couple of tasks to be worked on and the current system provides just a single thread, then tasks are assigned to the thread one by one. It can be pictorially depicted as

Multi-Threaded – In this environment, we used to have multiple threads which can take up these tasks and start working on that. It means we have a pool of threads (new threads can also be created based on the requirement and available resources) and bunch of tasks. So these thread can work on these as

Now, Let’s talk about Asynchronous model and how does it behave in single and multi-threaded environment.
Asynchronous Programming Model – In contrary to Synchronous programming model, here a thread once start executing a task it can hold it in mid, save the current state and start executing another task.

If our system is capable of having multiple threads then all the threads can work in asynchronous model as well

So till now we have discussed four scenarios –
- Synchronous Single Threaded
- Synchronous Multi-Threaded
- Asynchronous Single Threaded
- Asynchronous Multi-Threaded
Let’s discuss one more term – Concurrency.
Concurrency
In simple terms, concurrency means processing multiple requests at a time. As we have discussed two scenarios where multiple requests were getting processed, Multi-threaded programming and asynchronous model (single and multi-threaded both). In case of asynchronous model whether single or multi-threaded, multiple tasks are in progress at a time, some are in hold state and some are getting executed. There are many flavors of it but that is beyond the scope of this post.
As discussed earlier, the new age is of asynchronous programming. Why is it so important?
Benefits of Asynchronous Programming
There are two things which are very important for any application – Usability and Performance. Usability because say a user clicks on a button to save some data. This requires multiple smaller tasks like reading and populating data in internal object, establishing connection with SQL and saving it there etc. As SQL runs on another machine in network and runs under different process, it could be time consuming and may take bit longer. So, if the application runs on a single thread then the screen will be in hanged state till all the tasks completes which is a very bad user experience. That’s why nowadays many applications and new frameworks completely rely on asynchronous model.
Performance of an application is also very important. It has been seen that while executing a request, around 70-80% of the time gets wasted while waiting for the dependent tasks. So, it can be maximum utilized by asynchronous programming where once the task is passed to another process (say SQL), current thread saves the state and available to take another task. When the SQL task completes any thread which is free, can take it up further.
Asynchrony in ASP.NET
Async in ASP.NET can be a major boost in the throughput of your application. Let’s see, how IIS processes a request.

No comments:
Post a Comment