最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.NetCore利用BlockingCollection實(shí)現(xiàn)簡(jiǎn)易消息隊(duì)列

 更新時(shí)間:2018年09月04日 14:24:47   作者:范存威  
這篇文章主要介紹了.NetCore利用BlockingCollection實(shí)現(xiàn)簡(jiǎn)易消息隊(duì)列,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

消息隊(duì)列現(xiàn)今的應(yīng)用場(chǎng)景越來(lái)越大,常用的有RabbmitMQ和KafKa。

我們用BlockingCollection來(lái)實(shí)現(xiàn)簡(jiǎn)單的消息隊(duì)列。

BlockingCollection實(shí)現(xiàn)了生產(chǎn)者/消費(fèi)者模式,是對(duì)IProducerConsumerCollection<T>接口的實(shí)現(xiàn)。與其他Concurrent集合一樣,每次Add或Take元素,都會(huì)導(dǎo)致對(duì)集合的lock。只有當(dāng)確定需要在內(nèi)存中創(chuàng)建一個(gè)生產(chǎn)者,消費(fèi)者模式時(shí),再考慮這個(gè)類。

MSDN中的示例用法:

using (BlockingCollection<int> bc = new BlockingCollection<int>())
  {
    Task.Factory.StartNew(() =>
    {
      for (int i = 0; i < 1000; i++)
      {
        bc.Add(i);
        Thread.Sleep(50); 
      }
 
 
      // Need to do this to keep foreach below from hanging
      bc.CompleteAdding();
    });
 
 
    // Now consume the blocking collection with foreach.
    // Use bc.GetConsumingEnumerable() instead of just bc because the
    // former will block waiting for completion and the latter will
    // simply take a snapshot of the current state of the underlying collection.
    foreach (var item in bc.GetConsumingEnumerable())
    {
      Console.WriteLine(item);
    }
  }

實(shí)現(xiàn)消息隊(duì)列

用Vs2017創(chuàng)建一個(gè)控制臺(tái)應(yīng)用程序。創(chuàng)建DemoQueueBlock類,封裝一些常用判斷。

  • HasEle,判斷是否有元素
  • Add向隊(duì)列中添加元素
  • Take從隊(duì)列中取出元素

為了不把BlockingCollection直接暴漏給使用者,我們封裝一個(gè)DemoQueueBlock類

  /// <summary>
  /// BlockingCollection演示消息隊(duì)列
  /// </summary>
  /// <typeparam name="T"></typeparam>
  public class DemoQueueBlock<T> where T : class
  {
    private static BlockingCollection<T> Colls;
    public DemoQueueBlock()
    {

    }
    public static bool IsComleted() {
      if (Colls != null && Colls.IsCompleted) {
        return true;
      }
      return false;
    }
    public static bool HasEle()
    {
      if (Colls != null && Colls.Count>0)
      {
        return true;
      }
      return false;
    }
    
    public static bool Add(T msg)
    {
      if (Colls == null)
      {
        Colls = new BlockingCollection<T>();
      }
      Colls.Add(msg);
      return true;
    }
    public static T Take()
    {
      if (Colls == null)
      {
        Colls = new BlockingCollection<T>();
      }
      return Colls.Take();
    }
  }

  /// <summary>
  /// 消息體
  /// </summary>
  public class DemoMessage
  {
    public string BusinessType { get; set; }
    public string BusinessId { get; set; }
    public string Body { get; set; }
  }

添加元素進(jìn)隊(duì)列

通過(guò)控制臺(tái),添加元素

      //添加元素
      while (true)
      {
        Console.WriteLine("請(qǐng)輸入隊(duì)列");
        var read = Console.ReadLine();
        if (read == "exit")
        {
          return;
        }

        DemoQueueBlock<DemoMessage>.Add(new DemoMessage() { BusinessId = read });
      }

消費(fèi)隊(duì)列

通過(guò)判斷IsComleted,來(lái)確定是否獲取隊(duì)列

 Task.Factory.StartNew(() =>
      {
        //從隊(duì)列中取元素。
        while (!DemoQueueBlock<DemoMessage>.IsComleted())
        {
          try
          {
            var m = DemoQueueBlock<DemoMessage>.Take();
           Console.WriteLine("已消費(fèi):" + m.BusinessId);
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
          }
        }
      });

查看運(yùn)行結(jié)果

運(yùn)行結(jié)果

這樣我們就實(shí)現(xiàn)了簡(jiǎn)易的消息隊(duì)列。

示例源碼:簡(jiǎn)易隊(duì)列

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

平阳县| 德惠市| 铁岭市| 武义县| 海南省| 东光县| 阜阳市| 庐江县| 东兴市| 仁怀市| 荃湾区| 黎平县| 德钦县| 宁津县| 三河市| 临高县| 沐川县| 雷州市| 柳江县| 绥芬河市| 阿克陶县| 宁夏| 苍梧县| 新丰县| 石阡县| 长子县| 甘德县| 崇仁县| 阿勒泰市| 汶川县| 清河县| 凌云县| 衡水市| 新宾| 兰州市| 清丰县| 西盟| 平原县| 吴江市| 鄄城县| 正蓝旗|