Barrier

 internal class Program
 {
     static bool isOutput = true;
     static int index1 = 0;
     static int index2 = 100;
     static Barrier barrier = new Barrier(2, b =>
     {
         if (index1 == index2)
         {
             Console.WriteLine($"index1与index2相同,比对结束。");
             isOutput = false;
         }
         else
         {
             Console.WriteLine($"index1与index2不同!~");
         }
     });
     static void Main(string[] args)
     {
         var t = Task.Run(() => {
             while (isOutput)
             {
                 index1 = index1 + 2;
                 Console.WriteLine("index1:" + index1);
                 barrier.SignalAndWait();
             }
         });
         var t2 = Task.Run(() =>{
             while (isOutput)
             {
                 index2++;
                 Console.WriteLine("index2:" + index2);
                 barrier.SignalAndWait();
             }
         });

         Console.ReadLine();
     }
 }

 

posted @ 2024-10-23 14:06  Shapley  阅读(13)  评论(0编辑  收藏  举报