这是我的代码:
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);
}
}
}
}
两种方法Program1()
和Program2()
做同样的事情。 Program1()
调用CallToChildThread2()
将密钥值对输入到字典中,但是Program2()
调用CallToChildThread1()
将密钥值对并行输入并发字典。
输出如下: