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