【发布时间】:2011-06-21 00:49:36
【问题描述】:
我写了一些代码:
class Program
{
public const int count = 3000;
static List<int> list = new List<int>();
static void DoWork(int i)
{
list.Add(i);
}
static void Main(string[] args)
{
while (true)
{
Stopwatch s = new Stopwatch();
s.Start();
Parallel.For(0, count + 1, DoWork);
s.Stop();
Console.WriteLine("\n Elapsed: " + s.Elapsed.ToString());
Console.WriteLine("Expected: {0}", count + 1);
Console.WriteLine("count: {0}", list.Count);
Console.ReadKey();
list = new List<int>();
}
}
}
但结果不是预期的(
并非所有循环都在 Console.WriteLine 调用之前完成
使用 Parallel.For 有什么问题?
【问题讨论】: