C # multi-thread development 6: Use the lock statement to synchronize multiple threads

Source: Internet
Author: User

C # multi-thread development 6: Use the lock statement to synchronize multiple threads

When data is shared among multiple threads, you must consider thread synchronization. Ensure that only one thread accesses and changes the shared data at a time.

Using the lock statement in C #, you can easily set and unlock the lock so that only one thread can access and change the shared data at a time.

The following is an example of multi-threaded access to shared data. How can this problem be solved without synchronization?

Using System; using System. threading; namespace LockExamples {class Program {static int account = 1000; // account static int pocket = 0; // pocket static void Main (string [] args) {int threadCount = 10; var threads = new Thread [threadCount]; for (int I = 0; I <threadCount; I ++) {threads [I] = new Thread (DoWork); threads [I]. start () ;}for (int I = 0; I <threadCount; I ++) {threads [I]. join ();} Console. writeLine ("pocket =" + pocket);} public static void DoWork () {if (account >=1000) {Thread. sleep (10); // a small ATM account-= 1000; pocket + = 1000 ;}}}}

The sample code can be understood:A user withdraws money from his or her bank account ten times.

The logic for obtaining money is implemented by the following code.

If (account> = 1000) {Thread. Sleep (10); // a small ATM account-= 1000; pocket + = 1000 ;}

When the balance in your account is greater than or equal to 1000, you can take out 1000 and put it in your pocket.

Because only 1000 of the user's current account is left, even if the user takes 10 times, the final pocket should be only 1000. So what is the actual situation?

Please refer to the following execution result (the result may be one of 9000 ).

The result is 10000 !!!!!!!!

It is an exciting thing for a user to retrieve 1000 from an account with only 10000 yuan in balance.

But for banks, this is not a good thing, because as a result, the bank's money will be hollowed out by users sooner or later. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + authorization + Gz9s/W1eLR + bXEveG5 + 8TYo788L3A + authorization + 3 K/authorization + authorization ++ authorization + zzLfDzsq5ss/tyv2 + authorization = "brush: java; "> private static object o = new object (); lock (o) {if (account> = 1000) {Thread. sleep (10); // a small ATM account-= 1000; pocket + = 1000 ;}}The lock keyword is used in the code to lock the object o. When a thread gets the lock, other threads cannot get the lock again. Only when the current thread is unlocked can other objects get the lock again, in this way, only one thread can be locked at a time to access and modify shared data.

After the modified sample code is executed multiple times, the following results can be obtained each time.


The object locked by the lock statement must be of the reference type. Because the lock value type only locks a copy, it makes no sense.

Related Article

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.