February 12, 2017

Parallel Linq / Task-based Async Programming

What is Core ?
Each core displayed as a separate graph under the "CPU Usage History" section so count of graphs is the total core (processor) count for system.
--------------------------------------------------------------------------
Processor -> Core
Process - > Thread -> Handle
--------------------------------------------------------------------------

Parallel LINQ (PLINQ)

It's a way to run LINQ queries in parallel on multi-core/multi-processor systems, in order to speed them up.

IEnumerable<int> parallelResults =
from item in sourceData.AsParallel()
where item % 2 == 0
select item;
foreach (int item in parallelResults)
{
Console.WriteLine("Item {0}", item);
}

Result would not be come in proper sorting order but if we remove AsParallel(),It will give proper sequential order.

--------------------------------------------------------------------------

Task-based Async Programming


task resembles a thread or ThreadPool work item, but at a higher level of abstraction. The term task parallelism refers to one or more independent tasks running concurrently.

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Thread.CurrentThread.Name = "Main";

        // Create a task and supply a user delegate by using a lambda expression. 
        Task taskA = new Task( () => Console.WriteLine("Hello from taskA."));
        // Start the task.
        taskA.Start();

        // Output a message from the calling thread.
        Console.WriteLine("Hello from thread '{0}'.", 
                          Thread.CurrentThread.Name);
        taskA.Wait();
   }
}
// The example displays output like the following:
//       Hello from thread 'Main'.
//       Hello from taskA.
--------------------------------------------------------------------------

No comments: