【发布时间】:2013-03-09 15:27:57
【问题描述】:
Grant Crofton's answer对问题“无序线程问题”有评论:
“如果你移除锁,最终计数可能会少于 100。”
为什么?
这是上下文的代码
class Program
{
static object locker = new object();
static int count=0;
static void Main(string[] args)
{
for (int j = 0; j < 100; j++)
{
(new Thread(new ParameterizedThreadStart(dostuff))).Start(j);
}
Console.ReadKey();
}
static void dostuff(object Id)
{
lock (locker)
{
count++;
Console.WriteLine("Thread {0}: Count is {1}", Id, count);
}
}
}
【问题讨论】:
标签: c# .net multithreading asynchronous synchronization