Parallelism Technique in C#
Method 1 : Call Parallelism Technique
static void GenerateNumbers()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Method - Number: {0}", i);
Thread.Sleep(1000);
}
}
static void PrintCharacters()
{
string str = "ABCD";
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine("Method - Character: {0}", str[i]);
Thread.Sleep(1000);
}
}
static void PrintArray()
{
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8 };
foreach (int i in arr)
{
Console.WriteLine("Method3 - Array: {0}", i);
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
Parallel.Invoke(
new Action(GenerateNumbers),
new Action(PrintCharacters),
new Action(PrintArray)
);
Console.ReadLine();
}
Method 2 : Call Parallelism Technique
Parallel.Invoke(() =>
{
Console.WriteLine("Begin first task...");
},
() =>
{
Console.WriteLine("Begin second task...");
},
() =>
{
Console.WriteLine("Begin third task...");
}
);
Console.WriteLine("Returned from Parallel.Invoke");
No comments :
Post a Comment