首页 > 编程语言 > 详细

netcore 俩个线程乱序调整

时间:2021-06-15 16:27:09      阅读:19      评论:0      收藏:0      [点我收藏+]

 

 

 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 俩个线程乱序调整

原文:https://www.cnblogs.com/puzi0315/p/14885000.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!