我来自Threading with Parallel.For Adding Lists。对于链接中的示例,两个线程用于调用Parallel.Invoke
,这仍然很慢,所以我想在循环内做一些多线程,假设我不关心订单项的添加。
但parallel.for
没有作为excetd工作,并给indexoutofRange异常。
有人帮忙吗?
private static void Main(string[] args)
{
var dict1 = new Dictionary<int, string>();
var dict2 = new Dictionary<int, string>();
Stopwatch s = Stopwatch.StartNew();
Parallel.Invoke(() => FillDictionary(dict1, 10000000), () => FillDictionary(dict2, 10000000));
//FillDictionary(dict1, 10000000);
//FillDictionary(dict2, 10000000);
Console.WriteLine("test");
Console.WriteLine(s.Elapsed.TotalMilliseconds);
Console.ReadLine();
}
private static void FillDictionary(Dictionary<int, string> toFill, int itemCount)
{
//for (int i = 0; i < itemCount; i++)
//toFill.Add(i, "test" + i);
Parallel.For(0, itemCount, (i) => toFill.Add(i, "test" + i));
}