Introduction To
Parallel Programming
Parallel programming is a programming model wherein the
execution flow of the application is broken up into pieces that will be done at
the same time (concurrently) by multiple cores, processors, or computers for
the sake of better performance. Spreading these pieces across them can reduce
the overall time needed to complete the work and/or improve the user's
experience. However, this is not always the case as there are several pitfalls
you need to be aware of and we will certainly discuss them through the series.
Difference between asynchronous and parallel programming?
- When you run something asynchronously it means it is non-blocking, you execute it without waiting for it to complete and carry on with other things.
- Parallelism means to run multiple things at the same time, in parallel. Parallelism works well when you can separate tasks into independent pieces of work.
Asynchonous programming solves the problem of waiting around for an expensive operation to complete
before you can do anything else. If you can get other stuff done while you're waiting for the operation to
complete then that's a good thing. Example: keeping a UI running while you go and retrieve more data
from a web service.
Parallel programming is related but is more concerned with breaking a large task into smaller chunks that
can be computed at the same time. The results of the smaller chunks can then be combined to produce the
overall result.
Task Class
Task class Represents an asynchronous operation.
A Simple Example is below:
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
var arrTask = new List<Task>();
Task taskShowDate = Task.Run(() =>
{
ShowDate();
});
arrTask.Add(taskShowDate);
Task taskShowTime = Task.Run(() =>
{
ShowTime();
});
arrTask.Add(taskShowTime);
Task.WaitAll(arrTask.ToArray(), 10000000);
//Parallel.Invoke(ShowDate, ShowTime);
Console.ReadLine();
}
public static void ShowDate()
{
Console.WriteLine(DateTime.Now.ToLongDateString());
}
public static void ShowTime()
{
Console.WriteLine(DateTime.Now.ToLocalTime());
}
}
Task.WaitAll(): Waits for all of the provided Task objects to complete execution.
Why we need Tasks
It can be used whenever you want to execute something in parallel. Asynchronous implementation is easy in a task, using’ async’ and ‘await’ keywords.
here is another important link Introducing-task-programming
0 comments:
Post a Comment