【发布时间】:2015-07-04 18:45:38
【问题描述】:
我想在List<T> 上使用Parallel.ForEach 的强大功能来进行验证例程。迭代列表以确保属性不 Parallel.ForEach 的强大功能,并确保它在线程安全方面正常工作?
public static bool IsValid(List<Airport> entities)
{
bool isValid = true;
Parallel.ForEach<Airport>(entities, entity =>
{
// userId can't be less than 1
if (entity.userId < 1)
{
SiAuto.Main.LogMessage("Airport {0}: invalid userId {1}", entity.airportId, entity.userId);
isValid = false;
System.Diagnostics.Debugger.Break();
}
});
return isValid;
}
【问题讨论】:
-
由于您只将其设置为 false,我不明白这不是线程安全的。
-
但是你不想在第一个错误之后就中断循环吗?
-
列表有多大?
-
我不明白同步问题..这里不是线程安全的..某些线程可能将其设置为假..那又怎样?
标签: c# .net multithreading parallel-processing task-parallel-library