Lock lock problem

When multiple threads share a lock, lock the same time there is only one available to perform other thread is blocked to enter the queue, according to understand if the event is multi-threaded, then each event triggered by a function with the lock will be queued to wait, the more the more rows. Needed to be proved.

 public class Test
    {


       public void Fun1()
       {
           Task.Factory.StartNew(() => { 
           
           while(true)
           {
               Excute("线程1:");

           }
           
           
           });

       }
       public void Fun2()
       {
           Task.Factory.StartNew(() =>
           {

               while(true)
               {
                   Excute("线程2:");

               }


           });

       }

       public void Fun3()
       {
           Task.Factory.StartNew(() =>
           {

               while (true)
               {
                   Excute("线程3:");

               }


           });

       }
       object ob = new object();
       int A = 0;
       public void Excute(string id)
       {
           lock(ob)
           {
               Console.WriteLine(id+A.ToString());
               Thread.Sleep(2000);
               A++;
           }
          


       }

    }

Published 162 original articles · won praise 264 · Views 1.07 million +

Guess you like

Origin blog.csdn.net/u011555996/article/details/103935916