In all my previous projects I have used ThreadPool class for managed threading.I wanted to migrate the code to .net 4.5 framework and was looking at Task Parallel Library for threading.Task parallel library makes threading ridiculously easy, especially when the threads do not share resources.
Below are two implementation of parallel processing using the Parallel.Invoke function
using System;
using System.Threading.Tasks;
namespace TaskParallelLibraryExample
{
class Program
{
static void Main(string[] args)
{
Parallel.Invoke(() => RunMillionIterations1(1), () => RunMillionIterations2(2), () => RunMillionIterations3(3), () => RunMillionIterations4(4), () => RunMillionIterations5(5));
Console.Read();
}
private static void RunMillionIterations1(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}",iIndex,i.ToString());
}
}
private static void RunMillionIterations2(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
private static void RunMillionIterations3(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
private static void RunMillionIterations4(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
private static void RunMillionIterations5(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
}
}
using System;
using System.Threading.Tasks;
namespace TaskParallelLibraryExample
{
class Program
{
static void Main(string[] args)
{
Parallel.Invoke(() => RunMillionIterations1(1), () => RunMillionIterations1(2), () => RunMillionIterations1(3), () => RunMillionIterations1(4), () => RunMillionIterations1(5));
Console.Read();
}
private static void RunMillionIterations1(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}",iIndex,i.ToString());
}
}
}
}
Below are two implementation of parallel processing using the Parallel.Invoke function
using System;
using System.Threading.Tasks;
namespace TaskParallelLibraryExample
{
class Program
{
static void Main(string[] args)
{
Parallel.Invoke(() => RunMillionIterations1(1), () => RunMillionIterations2(2), () => RunMillionIterations3(3), () => RunMillionIterations4(4), () => RunMillionIterations5(5));
Console.Read();
}
private static void RunMillionIterations1(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}",iIndex,i.ToString());
}
}
private static void RunMillionIterations2(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
private static void RunMillionIterations3(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
private static void RunMillionIterations4(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
private static void RunMillionIterations5(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}", iIndex, i.ToString());
}
}
}
}
using System;
using System.Threading.Tasks;
namespace TaskParallelLibraryExample
{
class Program
{
static void Main(string[] args)
{
Parallel.Invoke(() => RunMillionIterations1(1), () => RunMillionIterations1(2), () => RunMillionIterations1(3), () => RunMillionIterations1(4), () => RunMillionIterations1(5));
Console.Read();
}
private static void RunMillionIterations1(int i)
{
for (int iIndex = 0; iIndex < 1000000; iIndex++)
{
Console.WriteLine("Iteration:{0} Code:{1}",iIndex,i.ToString());
}
}
}
}
Below are the screenshots of the processor utilization by the library.I have noticed a significant increase in performance due to a higher and more efficient utilization of the machine resources by the task parallel library.
No comments:
Post a Comment