(C #) using Queues to solve simple concurrency problems

Source: Internet
Author: User

There is a scene: a snapped up project, assuming that there are 5 items, who first grab who can buy, but if this moment (here at this moment is assumed to be the same time), there are 100 people to rob this product, if the use of the usual method will be what happens? You know, here is the problem of concurrency.

Usually we go to the supermarket shopping to checkout is the line, where we first let the snapping line team, according to time, who first click on the Snapping button who is in front, so that a queue, and then we deal with this queue, so there will be no concurrency problems. (At least you can handle this simple concurrency, which is not discussed too complex concurrency)

Case:

Requirements: There is an interface for publishing articles, each posting an article, invoking the interface. (There is no mass release, in order to explain this)

Create a handler class like this, BusinessInfoHelper.cs

namespace MyNamespace {//Queue Temp class public class Queueinfo {public string medias {get; set;}        public string Proids {get; set;}        public string Host {get; set;}        public string UserID {get; set;}    public string Feedid {get; set;} } public class Businessinfohelper {#region solve the phenomenon that the foreground page is stuck when the publication contains high quality media//principle: Use the Producer consumer mode to enter the listed column Operation publ        IC readonly static Businessinfohelper Instance = new Businessinfohelper ();        Private Businessinfohelper () {} private queue<queueinfo> listqueue = new queue<queueinfo> ();            public void Addqueue (string medias, String proids, String host, String userid, String feedid)//into row {            Queueinfo queueinfo = new Queueinfo ();            Queueinfo.medias = medias;            Queueinfo.proids = Proids;            Queueinfo.host = host;            Queueinfo.userid = userid;            Queueinfo.feedid = Feedid;        Listqueue.enqueue (Queueinfo);} public void Start ()//start {thread thread = new Thread (ThreadStart); Thread.            IsBackground = true; Thread.        Start (); private void ThreadStart () {while (true) {if (Listqueue.count >                    0) {try {scanqueue (); } catch (Exception ex) {Lo_loginfo.wllog (ex.                    ToString ()); }} else {//no tasks, rest 3 seconds thread.sleep (30                00);             }}}//method to execute private void Scanqueue () {while (Listqueue.count > 0) {try {//out of queue queueinfo Queueinfo = Lis                    Tqueue.dequeue ();   The queueinfo can be used, and there's something you want.                 The following is the handler//...                } catch (Exception ex) {throw; }}} #endregion}}

When the above page is written, you have to start the thread to do the task when the program starts running, so we can write it in the Application_Start of global:

Launch the release of Quality Media program MyNameSpace.BusinessInfoHelper.Instance.Start ();


There is a problem, if I finished processing a record in the queue, want to return the ID of this record, this program does not seem to complete, I used another method lock method, the method is locked, the specific following,

To define a global lock on a page:

Private static Object lockobject= new Object ();

This is called in the method:

Lock (LockObject) {//...}


If you do not use the second method of the global lock, I do not know if you have good solutions, if there is, you can keep abreast, thank you very much!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(C #) using Queues to solve simple concurrency problems

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.