.
, , , . . ThreadPool, Parallel Task.
I included a sample sample using the method Parallel.For. To make the selection understandable, let's say you have a list of objects that you want to clone and fall into a separate list. Suppose the cloning operation is expensive and that you want to parallelize the cloning of many objects. Here's how you do it. Pay attention to the placement and limited use of the keyword lock.
public static void Main()
{
List<ICloneable> original = GetCloneableObjects();
List<ICloneable> copies = new List<ICloneable>();
Parallel.For(0, 100,
i =>
{
ICloneable cloneable = original[i];
ICloneable copy = cloneable.Clone();
lock (copies)
{
copies.Add(copy);
}
});
}
source
share