为什么在下面的代码中使用lock是一种不好的做法,我假设这是一个不好的做法,基于这个SO问题的答案
private void DoSomethingUseLess()
{
List<IProduct> otherProductList = new List<IProduct>();
Parallel.ForEach(myOriginalProductList, product =>
{
//Some code here removed for brevity
//Some more code here :)
lock (otherProductList)
{
otherProductList.Add((IProduct)product.Clone());
}
});
}
Run Code Online (Sandbox Code Playgroud)
那边的答案提到这是不好的做法,但他们没有说明原因
注意:请忽略代码的用处,这只是为了示例目的,我知道它根本没用