C #, multi-thread deadlock is easy.

Source: Internet
Author: User
Original article: 2008-02-04

In the past few days, it is difficult to check C # multi-thread Dongdong to ensure that multiple threads do not die lock, but it is easy to deadlock. I tried to write a deadlock code. See the following. Environment :. NET 2.0 basic idea: Classic problem when learning OS, two threads seize two resources, thread 1 grab resource A, and resource B; thread 2 grab resource B, resource A is also required; the result is a deadlock.
 1  using System;
 2  using System.Collections.Generic;
 3  using System.Text;
 4  using System.Threading;
 5     
 6  namespace MultiThreadTest
 7  {
 8        class Program
 9     {
10            static void Main(string[] args)
11        {
12      Deadlock#region Deadlock
13                Deadlock DL = new Deadlock();
14                Thread DL1 = new Thread(DL.StartFromLocker1);
15                Thread DL2 = new Thread(DL.StartFromLocker2);
16     
17                DL1.Start();
18                DL2.Start();
19                #endregion
20            }
21     
23        }
24    
25        class Deadlock
26       {
27            object locker1 = new object();
28            object locker2 = new object();
29            string s1="StartFromLocker1", s2="StartFromLocker2";
30            string l1 = "locker1", l2 = "locker2";
31     
32            public void StartFromLocker1()
33           {
34                lock(locker1)
35               {
36                    Console.WriteLine("{0} got {1}", s1, l1);
37                    Thread.Sleep(500);
38                    Console.WriteLine("{0} waiting for {1}", s1, l2);
39                    lock(locker2)
40                    {
41                        Console.WriteLine("{0} got {1}", s1, l2);
42                        Thread.Sleep(500);
43                    }
44                }
45                Console.WriteLine("leaving {0}", s1); 
46            }
47     
48            public void StartFromLocker2()
49           {
50                lock (locker2)
51               {
52                    Console.WriteLine("{0} got {1}", s2, l2);
53                   Thread.Sleep(500);
54                    Console.WriteLine("{0} waiting for {1}", s2, l1);
55                    lock (locker1)
56                   {
57                        Console.WriteLine("{0} got {1}", s2, l1);
58                        Thread.Sleep(500);
59                    }
60                }
61                Console.WriteLine("leaving {0}", s2); 
62            }
63        };
64   }
Running result:
StartFromLocker1 got locker1StartFromLocker2 got locker2StartFromLocker1 waiting for locker2StartFromLocker2 waiting for locker1
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.