我想学习使用锁定在Parallel.ForEach. 我应该将整个代码块锁定在迭代中,还是应该在执行任何进程之前只锁定我想用作多线程安全的对象?
例如:
Parallel.ForEach(list, item =>
{
lock (secondList)
{
//consider other processes works in here
if (item.Active)
secondList.Add(item);
}
});
或者
Parallel.ForEach(list, item =>
{
//consider other processes works in here
if (item.Active)
{
lock (secondList)
secondList.Add(item);
}
});
慕斯王
肥皂起泡泡
qq_花开花谢_0
相关分类