class Program
    {
        public static long flgVal = 0;

        static void Main(string[] args)
        {
            int n = 100;
            Task.Run(() => DaMi(n));
            Task.Run(() => LaoShu(n));

            Console.Read();
        }

        public async static Task LaoShu(int n)
        {

            for (int i = 0; i < n; i++)
            {
                while (true)
                {

                    if (Interlocked.Read(ref flgVal) == 0)
                    {
                        break;
                    }
                }
                Console.Write("老鼠");
                Interlocked.Increment(ref flgVal);
            }
        }

        public async static Task DaMi(int n)
        {

            for (int i = 0; i < n; i++)
            {
                while (true)
                {

                    if (Interlocked.Read(ref flgVal) == 1)
                    {
                        break;
                    }
                }

                Console.Write("爱大米  ");
                Interlocked.Decrement(ref flgVal);


            }
        }







    }

netcore 俩个线程乱序调整

 

相关文章:

  • 2021-10-04
  • 2021-09-10
  • 2021-08-23
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
猜你喜欢
  • 2021-06-13
  • 2021-12-29
  • 2021-11-11
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2021-06-10
相关资源
相似解决方案