Thread, synchronization and lock ---- lock who you actually lock

Source: Internet
Author: User

The more obvious the advantage of threads in the multi-core era over the past month, the learning of multi-threaded programming is also on the agenda. But more and more people are stuck in the thread Quagmire, and finally they are totally confused. There are more and more deadlocks and more abnormal data. In the concurrency test, let one threadProgramEmployees are overwhelmed. "I have nothing to do in my own programming environment, and there will be no errors in single-step debugging. When two people are there, how can I not do it when multiple people are testing ?" Thread, synchronization and lock issues gradually emerged in front of every programmer.
Let's learn synchronization and lock together.

Lock is a well-known statement for every programmer, but how does it work?
Let's take a look at several cases to see what lock is.

 

 

1 Public   Class Threadtest
2 {
3 Private   Int I =   0 ;
4 Public   Void Test ()
5 {
6 Thread T1 =   New Thread (thread1 );
7 Thread T2 =   New Thread (thread2 );
8 T1.start ();
9 T2.start ();
10 }
11 Public   Void Thread1 ()
12 {
13 Lock ( This )
14 {
15 Console. writeline ( This . I );
16 Thread. Sleep ( 1000 );
17 Console. writeline ( This . I );
18 }
19 }
20 Public   Void Thread2 ()
21 {
22 Thread. Sleep ( 500 );
23 This . I =   1 ;
24 Console. writeline ( " Change the value in locking " );
25 }
26 }
27 Public   Class Threadtest2
28 {
29 Private   Int I =   0 ;
30 Public   Void Test ()
31 {
32 Thread T1 =   New Thread (thread1 );
33 Thread T2 =   New Thread (thread2 );
34 T1.start ();
35 T2.start ();
36 }
37 Public   Void Thread1 ()
38 {
39 Lock ( This )
40 {
41 Console. writeline ( This . I );
42 Thread. Sleep ( 1000 );
43 Console. writeline ( This . I );
44 }
45 }
46 Public   Void Thread2 ()
47 {
48 Lock ( This )
49 {
50 Thread. Sleep ( 500 );
51 This . I =   1 ;
52 Console. writeline ( " Can't change the value in locking " );
53 }
54 }
55 }

 

What are the differences between the two programs? Let's take a look. Another lock (this) in threadtest2.thread2 () produces different results.

In this case, we wanted to lock this object so that other threads could not operate on it, but the lock (this) is not the meaning of lock this as we think. attributes in this can still be changed by other threads. so what do we lock? YesCodeSegment, which is the code segment in the braces behind the lock. This code prevents multiple people from being unable to execute the lock (this) statement. What does this mean? It can be said that this knowledge marks the code domain. Let's look at thread2.thread2 in Case 2 to understand that the lock in thread2 will not start running until thread1 lock is released. It remains in the waiting state until it is released, this is the sign.

Okay. Let's take a look at how the lock code runs. the lock statement basically uses moniter. enter and moniter. exit, that is, when the lock (this) is executed, moniter. enter (this). Execute monitor at the end of braces. exit (this ). what does it mean? For any object, the first part of the memory places the addresses of all methods, and the second part places an index, it points to a syncblock in the syncblock cache area of the CLR. what does it mean? That is to say, when you execute monitor. Enter (object), if the index value of the object is negative, select a syncblock from the syncblock cache and put its address in the index of the object. In this way, the object-marked locking is completed. If other threads want to perform the monitor. Enter (object) operation again, they will get the index with a positive number of objects, and then wait. Until the index changes to negative, that is, the thread uses monitor. Exit (object) to change the index to negative.

If you understand monitor. the principle of "enter", of course, no more. of course, the value in the lock brackets does not mean to lock the entire object, but to modify one of its values so that other locks cannot lock him. This is the true face of the lock (object.

However, in actual use, the monitor is not recommended, but it is still locked. The monitor requires a lot of try catch to ensure security, but the lock helps us, and the lock looks more elegant.

How to Use lock in static methods? Because we do not have this available, we can use typeof (this). Type also has the corresponding method address and index, so he can also be used as a lock sign.

However, Microsoft does not advocate the use of public objects, typeof (), or strings because, if your public object is null in other threads and spam collected, unexpected errors will occur.

 

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.