Multi-Shadow Clone--c# using two threads (scramble for shared resources)

Source: Internet
Author: User

As long as the server is able to handle it, we can open any thread to work at the same time to improve efficiency, however

Two threads scrambling for resources can cause data clutter.

For example:

 Public classMyfood { Public Static intLast {Get;Set; }  PublicMyfood () { last= -; }      Public voidEatfood () {intFoods =Last ; Thread.Sleep ( +); Last= Foods-1;        Console.WriteLine (last); }         Public voidEatmuchfood () {intFoods =Last ; Thread.Sleep ( the); Last= Foods-Ten;        Console.WriteLine (last); }    }

This defines a Myfood class, which has a static variable last, which stores the remaining food. Then use the constructor to assume that the initial food is 500.

Two methods, namely eating a food and eating 10 food, using thread.sleep () to simulate eating things takes time.

OK, now it's time to eat something:

   New New Thread (new new Thread (new ThreadStart (E. Eatmuchfood)); Th1. Start ();    // Wait here for the first thread to end, then go back to Th2. Start ();  //489 Console.readkey ();

Without any problems, it takes 1 seconds to finish eating the first food and 499 remaining. Then take 3 seconds to eat 10 food, the remaining 489.

If we don't add Th1. Join (), which is the normal multi-threading, to see what happens:

   New New Thread (new new Thread (new// 499 // 490 Console.readkey ();

You can see that two outputs are 499 and 490, respectively, and there's a problem here.

Eatfood method, take out the remaining last after resting for 1 seconds, and then 1 after the number is assigned to the last. Exactly, during this 1-second period, our Th2 came in to call the Eatmuchfood method, it is also the total number, and this total is the Eatfood method has not-1 of the total, so it takes 500 instead of 499.

That's why the problem happened.

So on this shared resource with total number, we want only one thread to access it at the same time (to make sure the data is correct), and one of the solutions is lock:

 Public classMyfood {Private Static ReadOnly ObjectLockhelper =New Object();  Public Static intLast {Get;Set; }  PublicMyfood () { last= -; }         Public voidEatfood () {Lock(lockhelper) {intFoods =Last ; Thread.Sleep ( +); Last= Foods-1;        } Console.WriteLine (last); }         Public voidEatmuchfood () {Lock(lockhelper) {intFoods =Last ; Thread.Sleep ( the); Last= Foods-Ten;        } Console.WriteLine (last); }    }

This adds a static read-only private variable called Lockhelper, then modifies two ways to eat, plus a lock on the lockhelper.

In this way, each time it executes, it will determine if the lockhelper is locked, and if not, enter the lock code block to Lockhelper and execute the code. The lock code block will automatically release the locks.

This ensures that there is only one thread at a time when the last change is made.

In this example, the lock (this) is used directly, this represents an instance of the class Myfood E.

Multi-Shadow Clone--c# using two threads (scramble for shared resources)

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.