欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用队列(Queue)解决简单的并发问题

程序员文章站 2023-11-16 23:16:34
本文通过实例,更具体的讲解了队列,队列(queue)代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表...

本文通过实例,更具体的讲解了队列,队列(queue)代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队。

有一个场景:一个抢购的项目,假设有5件商品,谁先抢到谁可以买,但是如果此时此刻(这里的此时此刻假设是相同的时间),有100人去抢这个商品,如果使用平时的方法会出现什么情况呢?你懂的,这里所说是就是有关并发的问题。

平时我们去超市购物去结账的时候就是排队,这里我们先让抢购人排好队,按时间,谁先点击的抢购按钮谁就排在前面,这样就形成了一个队列,然后我们再对这个队列处理,这样就不会出现并发的问题了。(至少可以处理这样简单的并发,这里不讨论太复杂的并发)

案例:

要求:有一个发布文章的接口,每发布一篇文章,调用一下接口。(这里不用批量发布,为了讲解这个)

建立一个这样的处理程序类,businessinfohelper.cs

 

namespace mynamespace  
 
{ 
  //队列临时类 
  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 解决发布时含有优质媒体时,前台页面卡住的现象 
    //原理:利用生产者消费者模式进行入列出列操作 
 
    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) //入列 
    { 
      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()//启动 
    { 
      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 
        { 
          //没有任务,休息3秒钟 
          thread.sleep(3000); 
        } 
      } 
    } 
 
    //要执行的方法 
    private void scanqueue() 
    { 
      while (listqueue.count > 0) 
      { 
        try 
        { 
          //从队列中取出 
          queueinfo queueinfo = listqueue.dequeue(); 
 
          //取出的queueinfo就可以用了,里面有你要的东西 
          //以下就是处理程序了 
          //。。。。。。 
 
        } 
        catch (exception ex) 
        { 
          throw; 
        } 
      } 
    } 
 
 
    #endregion 
  } 
} 
 

以上页面写好后,在程序开始运行时就得启动这个线程去不停的处理任务,那么我们在global的application_start里可以这样写:

//启动发布优质媒体程序 
mynamespace.businessinfohelper.instance.start(); 

有一个问题出来了,如果我处理完队列中的一条记录后,想返回这条记录的id,这个程序好像不能完成,我就使用了另一个方法 lock方法 ,把方法锁定,具体的如下,

在页面中定义全局的锁:

private static object lockobject= new object(); 
 
在方法中这样调用:

lock(lockobject) 
 

 
//........ 
 

以上是本文的全部内容,希望对大家进一步学习队列有所帮助