C uses the queue of Queue to solve simple concurrency problems

  • 2021-07-22 11:07:13
  • OfStack

In this paper, through an example, more specific explanation of the queue, queue (Queue) represents a first-in first-out object set. Queues are used when you need first-in, first-out access to items. When you add 1 item to the list, it is called queuing, and when you remove 1 item from the list, it is called queuing.

There is a scenario: a snapped-up project, assuming that there are 5 items, whoever grabs them first can buy them, but if there are 100 people grabbing this item at this moment (assuming the same time here), what will happen if the usual method is used? You know, what we are talking about here is the problem of concurrency.

Usually, when we go shopping in the supermarket to check out, we queue up. Here, we let the snapping people line up first. According to the time, whoever clicks the snapping button first will line up first, thus forming a queue, and then we will deal with this queue, so that there will be no concurrent problems. (At least this simple concurrency can be handled, and too complicated concurrency will not be discussed here.)

Case:

Requirements: There is an interface for publishing articles, and every time an article is published, the interface under 1 is called. (There is no need to publish in batches here, in order to explain this)

Create one such handler class, BusinessInfoHelper. cs


namespace MyNameSpace  
 
{ 
  // Queue temporary 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 front page is stuck when publishing contains high-quality media  
    // Principle: Use producer-consumer mode to carry out listing operation  
 
    public 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) // Column entry  
    { 
      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 task, rest 3 Seconds  
          Thread.Sleep(3000); 
        } 
      } 
    } 
 
    // Method to execute  
    private void ScanQueue() 
    { 
      while (ListQueue.Count > 0) 
      { 
        try 
        { 
          // Take out from the queue  
          QueueInfo queueinfo = ListQueue.Dequeue(); 
 
          // Taken out queueinfo You can use it. There is something you want in it  
          // Here's the handler  
          // . . . . . .  
 
        } 
        catch (Exception ex) 
        { 
          throw; 
        } 
      } 
    } 
 
 
    #endregion 
  } 
} 
 

After the above page is written, when the program starts running, it has to start this thread to process tasks without stopping, so we can write this in Application_Start of Global:

//Launch the release quality media program
MyNameSpace.BusinessInfoHelper.Instance.Start();

There is a problem, if I finish processing a record in the queue, want to return the record ID, this program seems to be unable to complete, I use another method Lock method, the method locked, as follows,

Define global locks in the page:

private static object lockObject= new Object();

Called in the method as follows:

lock(lockObject)

{

//........

}

The above is the whole content of this article, hoping to help everyone learn the queue one step further


Related articles: