. Net multithreading note (6): Thread Synchronization

Source: Internet
Author: User

Synchronization block mechanism:

    • Initialize the synchronization block array when. NET is loaded.
    • Each object allocated to the stack contains two additional fields, one of which is a storage type pointer, and the other is a synchronized block index, which is initially assigned-1.
    • When a thread tries to use this object for synchronization, it will check the synchronization index of this object. If the index is negative, a synchronization block is searched for or created in the synchronization block array, and the index value of the synchronization block is written into the synchronization index of the object. If the synchronization index of the object is not negative, find the synchronization block of the object and check whether other threads are using the synchronization block. If yes, enter the waiting status, if not, it is declared that the synchronization block is used.
    • When an Object exits synchronization, the synchronization index of the object is assigned a value of-1, and the synchronization block in the corresponding number of synchronization blocks is considered no longer used.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace multithreadtest {class program {static void main (string [] ARGs) {console. writeline ("test static method synchronization? "); For (INT I = 0; I <5; I ++) {thread t = new thread (lock. add1); T. start ();} thread. sleep (10*1000); console. writeline ("test to member method synchronization"); lock L = new lock (); For (INT I = 0; I <5; I ++) {thread t = new thread (L. add2); T. start ();} console. read () ;}} public class lock {// used to synchronize static methods private static object O1 = new object (); Private Static int I1 = 0; // used to synchronize the member method private object O2 = new object (); Private int I2 = 0; public static void Add1 (object state) {lock (O1) {console. writeline ("before Add: I1 = {0}", i1); thread. sleep (1000); I1 ++; console. writeline ("after add: I1 = {0}", i1) ;}} public void Add2 (object state) {lock (O2) {console. writeline ("before Add: I1 = {0}", I2); thread. sleep (1000); I2 ++; console. writeline ("after add: I1 = {0}", I2 );}}}}

Note: For static method synchronization, static private reference members are generally used. For member method synchronization, private reference members are generally used. Objects of the value type are not configured on the stack and no index fields are synchronized. Therefore, they cannot be used for synchronization.

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.