what is the relation between Asynchronous and para

2019-02-03 07:44发布

I am getting confused as asynchronous programming is a way to execute a block of code asynchronously, that calls a method and doesn't wait for the result. In the same way, parallel programming is a way to execute more than one task simultaneously, but all those tasks are executed asynchronously. So wondering/confused what is the relationship between these two programming paradigms in c#.

6条回答
叛逆
2楼-- · 2019-02-03 07:45

finally :

use Parallel programming for CPU Intensive solutions. use Asynchronous programming for IO Bound solutions.

查看更多
何必那么认真
3楼-- · 2019-02-03 07:51

Parallel programming mostly concerns about improving PERFORMANCE of the system.

Asynchronous programming mostly concerns about improving RESPONSIVENESS of the system.

Threads, tasks etc. are techniques to achieve both async and parallel programming.

查看更多
叛逆
4楼-- · 2019-02-03 07:58

In general asynchronous means execute when ever possible, Parallel means execute right away by creating a new execution thread.
Here the link

查看更多
smile是对你的礼貌
5楼-- · 2019-02-03 08:08

One of the most simplest ways I was able to understand Parallel and Asynchronous programming was by thinking of the "boiling an egg" scenario taken from Pluralsight which I've changed slightly to incorporate threads.

Parallel Programming

  • Hob: CPU
  • Multiple Pots: Thread
  • Multiple Eggs: Parallel Task

You have multiple egg's (Tasks) which need to be boiled at the same time. In this example, the hob will be the CPU, each pot which is boiling one egg will be a single thread and the eggs are the parallel task. I can boil multiple eggs (Tasks) by adding more pots (Threads) to the hob (CPU) at the same time. Without the use of parallel programming, I could only boil 1x egg (Task) at one time as you would only have 1x pot (Thread) to work with which would ultimately slow down the process of boiling the eggs.

Asynchronous Programming

  • Egg Timer: Asynchronous Task
  • Hob: CPU
  • Multiple Pots: Thread
  • Multiple Eggs: Task

Following on from the example above, you now decide that you only want know when all of the eggs have been boiled rather than when each single egg has finished boiling. To do this, you would use an asynchronous task which in this instance will play as the egg timer. The egg timer (Asynchronous Task) will update you when the all of the eggs (Tasks) have been completed so your free to do other things up until this point.

查看更多
干净又极端
6楼-- · 2019-02-03 08:11

Parallel programming is a technique where we use multiple threads to execute a task faster. This means that on modern multi-core architectures we can utilize more of the resources available to perform a task.

A great example of this is sorting a list using quicksort.

Normally with parallel programming performance is important and all the threads are working to a common goal.

Asynchronous programming is subtly different. This normally involves longer running tasks and tasks which are perhaps waiting on some kind of external stimuli. A good example of this is to perform a large calculation in a background thread so that the UI remains responsive. With asynchronous code we are normally talking about code which executes at a different rate to our main application.

查看更多
再贱就再见
7楼-- · 2019-02-03 08:11

Parallel programming means executing operations At the same time using multiple threads, processes cpu's and or cores.

Asynchronous programming as you said means to fire a request and provide a callback mechanism to receive the response.

查看更多
登录 后发表回答