Sometimes we need to download a large file, or we need to build a software or game like Counter Strike or Valorant that it's functions works simultaneously or need to work at the same time. For example if you are playing Competitive Valorant and you throw a flashback to blind your enemy, everyone are blind in the same time. In this case, your game does complex calculation and need to blind your enemy at the same time. Without an asynchronous programming, it is not gonna happen.
So asynchronous programming is the way of executing programming code in a thread without having to wait for IO bound or CPU bound task to finish. IO bound operations could be file-system accesses, HTTP requests, API calls, or database queries. CPU bound operations would be actions like encrypting a data, complex calculations, image or document management.
One of the ideas of asynchronous programming is to create awaitable tasks, so that we don't block the execution of our application. We can call an asynchronous method and get a task object that represents it. After we execute those operations, we await the asynchronous task, which may already be finished or not. If the execution is finished, we will get result from the task and use it in the next dependant operations or start next operations.
It has benefits:
- UI of our application is responsive
- Performance of our application has improved
- Avoid thread pool starvation
But also it has drawbacks:
- Code will be more complex and harder to maintain
- Increased memory allocation, so some objects should stay longer while awaiting other code to be executed. That's why you need a stronger PC to play FPS games
- It can be harder to find bugs in code
Top comments (0)