public void Compute(DeadLockExample deadLockExample)
 {
     lock (_lockObject)
     {
         Thread.Sleep(1);
         lock (deadLockExample._lockObject)
         {
             Console.WriteLine("Thread number started {0}", Thread.CurrentThread.ManagedThreadId);
             _number = deadLockExample.GetNumber();
         }
     }
 }
示例#2
0
        public static void Run()
        {
            var lockExample1 = new DeadLockExample(1);
            var lockExample2 = new DeadLockExample(2);

            for (int i = 0; i < 100; i++)
            {
                Thread thread = new Thread(() => {
                    if (i % 2 == 0)
                    {
                        lockExample1.Compute(lockExample2);
                    }
                    else
                    {
                        lockExample2.Compute(lockExample1);
                    }
                });
                thread.Start();
            }

            Console.WriteLine("lockExample1 {0}", lockExample1.GetNumber());
            Console.WriteLine("lockExample2 {0}", lockExample2.GetNumber());
        }