一个关于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
求解释为什么?
              
------解决方案--------------------第二次 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)