一个关于C#线程的低级问题
class Program
{
static private bool done;
static void Main(string[] args)
{
Thread t = new Thread(Go);
t.Start();
Go();
}
static void Go()
{
//Console.WriteLine("1");
if (!done)
{
Console.WriteLine("Done");
done = true;
Console.WriteLine("2");
}
}
}
该程序的运行结果是
Done
2
Done
2
如果去掉注释,运行结果就变成:
1
Done
2
1
求解释为什么?
线程 c#
[解决办法]
第二次 done= true了
[解决办法]
class Program
{
static private bool done;
static void Main(string[] args)
{
//Thread t = new Thread(Go);
// t.Start();
Go();
Go();
Console.ReadLine();
}
static void Go()
{
// Console.WriteLine("1");
if (!done)
{
Console.WriteLine(done.ToString());
done = true;
Console.WriteLine("2");
}
}
}