我正在尝试利用 .NET Framework 4.0 中的并行 for 循环。但是我注意到,我在结果集中缺少一些元素。
我有如下代码片段。lhs.ListData是一个可以为空的双精度列表,而rhs.ListData是一个可以为空的双精度列表。
int recordCount = lhs.ListData.Count > rhs.ListData.Count ? rhs.ListData.Count : lhs.ListData.Count;
List<double?> listResult = new List<double?>(recordCount);
var rangePartitioner = Partitioner.Create(0, recordCount);
Parallel.ForEach(rangePartitioner, range =>
{
for (int index = range.Item1; index < range.Item2; index++)
{
double? result = lhs.ListData[index] * rhs.ListData[index];
listResult.Add(result);
}
});
lhs.ListData的长度为 7964,rhs.ListData的长度为 7962。当我执行“ * ”操作时,listResult 的输出只有 7867。两个输入列表中都有空元素。
我不确定执行期间发生了什么。为什么我在结果集中看到更少的元素有什么原因吗?请指教...