Task Parallel Libraray vs Threading
1. Create a Console Application in VS2010 name TaskParallelLibraray
2. Include Following Name Spaces in Program.CS
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics; using System.Collections; using System.Threading.Tasks;3 Write Below code inside Class Program in Program.CS
static void Main(string[] args)
{
try
{
// CREATE A TREE WITH ~1023 NODES
Tree tree = Tree.CreateSomeTree(10, 1);
// START THE STOP WATCH
Stopwatch sw = Stopwatch.StartNew();
//WALK THE TREE
WalkTree(tree);
// GET THE TIME ELAPSED
Console.WriteLine("Time Elapsed = " + sw.ElapsedMilliseconds.ToString());
// WAIT TO SEE RESULTS
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();
}
}
// WALK THE TREE USING RECURSION
static void WalkTree(Tree tree)
{
//RETURN WHEN THERE ARE NO MORE CHILDRENS
if (tree == null) return;
#region USING TRADITIONAL SEQUENTIAL APPROACH
WalkTree(tree.Left);
WalkTree(tree.Right);
#endregion
#region USING THREADING
// ************ PROCESS EVERY NODE USING THREADING
// THIS MAY GIVE FOLLOWING EXCEPTION
// An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll
// BECAUSE WHENEVER NEW THREAD CREATED IT REQUIRE TO STORE PREVIOS THREAD STATE WHICH IS MORE MEMORY CONSUMING IN THIS EXAMPLE
//Thread left = new Thread(() => WalkTree(tree.Left));
//left.Start();
//Thread right = new Thread(() => WalkTree(tree.Right));
//right.Start();
//// WAIT/JOIN ON BOTH OF THEM TO COMPLETE
//left.Join();
//right.Join();
//************ Process every node using thread - End
#endregion
#region USING TASKS PARALLEL LIBRARY THAT IS USING TASK
//Task left = new Task(() => WalkTree(tree.Left));
//left.Start();
//Task right = new Task(() => WalkTree(tree.Right));
//right.Start();
//left.Wait();
//right.Wait();
#endregion
ProcessItem(tree.Data);
}
// SPINWAIT PUTS PROCESSOR IN TIGHT LOOP, WITH LOOP COUNT SPECIFIED IN ITERATION PARAMETER
private static int ProcessItem(int p)
{
// THIS IS WHERE COMPUTE/BUSINESS LOGIC GOES
Thread.SpinWait(4000000); // CONTRAST WITH THREAD.SLEEP
return p; // not used
}
4. Add Class name Tree.cs and paste following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TaskParallelLibraray
{
public class Tree
{
public Tree Left = null;
public Tree Right = null;
public int Data = 0;
// Tree of depth 3 and start of 1
// each node will have children left and right
// value will increase based on depth
// 1
// 2 2
// 3 3 3 3
//4 4 4 4 4 4 4 4
internal static Tree CreateSomeTree(int depth, int start)
{
Tree root = new Tree();
root.Data = start;
if (depth > 0)
{
root.Left = CreateSomeTree(depth - 1, start + 1);
root.Right = CreateSomeTree(depth - 1, start + 1);
}
return root;
}
}
}
5. Run this program in a machine having dual core or core to duo processor and c the out put
Almost all our servers are having multicore processor :D
so performance will be great on servers rather than out Desktops
Reference :MSDN
Recent Comments