This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
namespace ConsoleApplication2
{
public class c_Thread
{
public bool ThreadOngoing;
public c_Thread()
{
ThreadOngoing = false;
}
public void CallToChildThread1(string key, ref List<int> nums, ref ConcurrentDictionary<string, List<int>> container)
{
container.TryAdd(key, nums);
}
public void CallToChildThread2(string key, ref List<int> nums, ref Dictionary<string, List<int>> container)
{
container.Add(key, nums);
}
}
class Program
{
static void Main(string[] args)
{
RunProgram();
Console.ReadKey();
}
static void RunProgram()
{
System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
Console.WriteLine("Normal");
Program1();
sw.Stop();
Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);
sw.Reset();
sw.Start();
Console.WriteLine("Parallel");
Program2();
sw.Stop();
Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);
}
static void Program1()
{
c_Thread cc = new c_Thread();
Dictionary<string, List<int>> container1 = new Dictionary<string, List<int>>();
List<int> aList = new List<int>();
List<int> bList = new List<int>();
string printLine;
aList.Add(2);
aList.Add(4);
aList.Add(2);
aList.Add(2);
aList.Add(4);
aList.Add(2);
aList.Add(2);
aList.Add(4);
aList.Add(2);
aList.Add(2);
aList.Add(4);
aList.Add(2);
bList.Add(1);
bList.Add(3);
bList.Add(1);
bList.Add(1);
bList.Add(3);
bList.Add(1);
bList.Add(1);
bList.Add(3);
bList.Add(1);
bList.Add(1);
bList.Add(3);
bList.Add(1);
cc.CallToChildThread2("param1", ref aList, ref container1);
cc.CallToChildThread2("param2", ref bList, ref container1);
foreach (string key in container1.Keys)
{
printLine = key + ": ";
foreach (int val in container1[key])
printLine += val + " ";
Console.WriteLine(printLine);
}
}
static void Program2()
{
c_Thread cc = new c_Thread();
ConcurrentDictionary<string, List<int>> container1 = new ConcurrentDictionary<string, List<int>>();
List<int> aList = new List<int>();
List<int> bList = new List<int>();
string printLine;
aList.Add(2);
aList.Add(4);
aList.Add(2);
aList.Add(2);
aList.Add(4);
aList.Add(2);
aList.Add(2);
aList.Add(4);
aList.Add(2);
aList.Add(2);
aList.Add(4);
aList.Add(2);
bList.Add(1);
bList.Add(3);
bList.Add(1);
bList.Add(1);
bList.Add(3);
bList.Add(1);
bList.Add(1);
bList.Add(3);
bList.Add(1);
bList.Add(1);
bList.Add(3);
bList.Add(1);
Thread myNewThread1 = new Thread(() => cc.CallToChildThread1("param1", ref aList, ref container1));
myNewThread1.Start();
Thread myNewThread2 = new Thread(() => cc.CallToChildThread1("param2", ref bList, ref container1));
myNewThread2.Start();
myNewThread1.Join();
myNewThread2.Join();
foreach (string key in container1.Keys)
{
printLine = key + ": ";
foreach (int val in container1[key])
printLine += val + " ";
Console.WriteLine(printLine);
}
}
}
}
The two methods Program1()
and Program2()
do the same thing. Program1()
calls CallToChildThread2()
to input a key value pair into the dictionary however, Program2()
calls CallToChildThread1()
to input key value pairs into a concurrentdictionary parallely.
The output is the following: