Synchronous and Asynchronous Methods - HSM
Synchronous and Asynchronous Methods - HSM
Table of Contents
1. Introduction
2. Synchronous Methods
1. Definition
2. Use Cases
3. Synchronous Workflow
4. Example of Synchronous Method (C# / ASP.NET Core)
3. Asynchronous Methods
1. Definition
2. Use Cases
3. Asynchronous Workflow
4. Example of Asynchronous Method (C# / ASP.NET Core)
4. Synchronous vs. Asynchronous: Key Differences
5. Workflow Diagram: Synchronous vs. Asynchronous
1. Introduction
2. Synchronous Methods
2.1. Definition
A synchronous method is one where operations are executed sequentially. Each task waits for
the previous one to complete before proceeding, which can lead to performance bottlenecks
when performing tasks such as database access, file I/O, or network calls.
In synchronous methods, each step in the code must wait for the previous step to finish before
moving forward, causing potential delays, especially in I/O-bound operations
● Thread : A thread is the smallest unit of execution in a process that can perform tasks
independently in CPU and Memory.
Thread Lifecycle:
3. Asynchronous Methods
3.1. Definition
An asynchronous method allows tasks to run concurrently without blocking the main execution
thread. It enhances application responsiveness, particularly for I/O-bound operations, such as
accessing databases or external APIs.
In asynchronous methods, tasks can be initiated and the program can continue executing other
tasks while waiting for the result, thus improving performance, especially in I/O-heavy scenarios.
This example uses the async and await keywords to execute the GetAllProductsAsync
method without blocking the calling thread.
● Synchronous Workflow:
1. Task A → waits until completed.
2. Task B → waits until Task A finishes.
3. Task C → waits until Task B finishes.
● Asynchronous Workflow:
1. Task A → starts and allows other tasks to execute.
2. Task B → executes while Task A is still running.
3. Task C → executes without waiting for Task A or B to finish.
● Kestrel Server: In ASP.NET Core, the Kestrel web server handles incoming HTTP
requests. When Kestrel receives more requests than there are available threads, it
places the requests into an internal queue.
● Request Queue: This queue is managed within the server infrastructure, holding
requests until threads become available.
● Work Queue: The .NET thread pool uses a work queue to handle tasks and requests. If
the thread pool is at capacity, new tasks or requests are placed into this queue.
- Controllers
- ProductsController.cs
- Services
- ProductService.cs
- Repositories
- ProductRepository.cs
- Models
- Product.cs
6.2. Controller Example: Synchronous vs. Asynchronous
Synchronous Version:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IProductService _productService;
[HttpGet]
public IActionResult GetAll()
{
var products = _productService.GetAll();
return Ok(products);
}
}
Asynchronous Version:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IProductService _productService;
[HttpGet]
public async Task<IActionResult> GetAllAsync()
{
var products = await _productService.GetAllAsync();
return Ok(products);
}
}
7. Conclusion
Synchronous and asynchronous methods both have distinct use cases, especially in modern
web applications that rely on handling multiple requests and responses efficiently. In C# /
ASP.NET Core, the combination of asynchronous programming, MVC architecture, dependency
injection, and repository patterns enables developers to write scalable, maintainable, and
performant applications.