C # face question--recursive containing lock

Source: Internet
Author: User

Do iOS also has 1 years, some of the C # things have forgotten, in recent days also intends to revisit, can not learn the iOS to abandon the C #, both to grasp, a fine more specialized. At present, C # Just relive, the focus is the web this piece. Today, mainly think of the previous done before the face test, although the problem is more abnormal, but also has its significance.

 Public void Test (int  i)  {     lock(this)    {       if  ten)      {           i--;           Test (i);}}  }

Based on the knowledge of thread safety, analyze the following code to see if a deadlock occurs when the test method is called i>10? and give a brief explanation of the reason.

Must have done a C # interview will encounter this problem, I remember my first interview to do the time is also wrong, thinking that the recursion contains the lock, will definitely deadlock.

At that time went back to his own knock, and then eggs, there is no deadlock. Later saw the University of C # textbook, and Baidu, found himself to understand the lock wrong. I remember that the textbook example was an example of a bank withdrawal. And in this problem, has been in the main thread, and did not open a second thread, how can the lock happen. I remember there is also a lock mechanism in the database, the lock is mainly to solve the problem of concurrency, the main reason for the lock generation is two: at the same time to compete for the same resource A, b at the same time C, contention for resources to form a ring, a to B,b to C,c to a.

About locks in the database time I can write a blog alone, this is not written.

C # face question--recursive containing lock

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.