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