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#.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
finally :
use Parallel programming for CPU Intensive solutions. use Asynchronous programming for IO Bound solutions.
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.
In general asynchronous means execute when ever possible, Parallel means execute right away by creating a new execution thread.
Here the link
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
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
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.
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.
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.