Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

Are asynchronous and parallel programming the same?

Ayyaz Sheikh

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.

Asynchronous programming

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.

Syntax in C#:

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.

Parallel programming

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.



Ayyaz Sheikh
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring