【发布时间】:2012-03-07 18:45:01
【问题描述】:
我写了以下几行代码:
private static List<Int32> GetRandomList_Serial()
{
List<Int32> returnValue = new List<int>();
Random random = new Random();
for (int i = 0; i < 10000000; i++)
{
returnValue.Add(random.Next());
}
returnValue.Sort();
return returnValue;
}
然后我写了这段代码:
private static List<Int32> GetRandomList_Parallel()
{
List<Int32> returnValue = new List<int>();
Random random = new Random();
Parallel.For(0, 10000000, y =>
{
returnValue.Add(random.Next());
});
returnValue.Sort();
return returnValue;
}
串行工作正常,并行抛出此异常:
System.ArgumentException 未被用户代码处理
目标数组不够长。检查 destIndex 和长度,以及数组的下限。
有人知道为什么吗?
【问题讨论】:
-
并行化这样一个微不足道的操作是没有意义的。此外,您无法真正并行化涉及共享资源(如列表)的操作。
标签: .net task-parallel-library