In present days, async and parallel programming are spoken of interchangeably. But fundamentally, they both are a bit different. Asynchronous programming is used to enhance the usability of processes, while parallel programming aims for the computing/ processing efficiency.
Async achieves parallelism by executing an independent piece of code separately from the rest of the process. Typically, asynchronous programs run on a single core and perform context switching. It executes in such a way that it does not block the flow of the process.
Keyword Async
tells the compiler that this function/ method contains await
statement(s).
static async Task Main(string[] args)
{
Coffee cup = PourCoffee();
Console.WriteLine("coffee is ready");
Egg eggs = await FryEggsAsync(2);
Console.WriteLine("eggs are ready");
Bacon bacon = await FryBaconAsync(3);
Console.WriteLine("bacon is ready");
}
The await
keyword does the magic and lets the next independent code execute rather than blocking them.
In the above code snippet, a call to the FryEggsAsync
will not block the execution of FryBaconAsync
.
It is real parallelism. It means that you have multi-cores to run your application on. You write your application focusing on the maximum utilization of your CPUs. Each process runs on a separate core.
Free Resources