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

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

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

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

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

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

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)建一個控制臺應(yīng)用程序。創(chuàng)建DemoQueueBlock類,封裝一些常用判斷。

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

為了不把BlockingCollection直接暴漏給使用者,我們封裝一個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ì)列

通過控制臺,添加元素

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

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

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

通過判斷IsComleted,來確定是否獲取隊(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)了簡易的消息隊(duì)列。

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

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

相關(guān)文章

最新評論

桐城市| 抚顺县| 门源| 涞源县| 株洲市| 桃江县| 枣庄市| 上犹县| 鹤庆县| 普洱| 格尔木市| 新疆| 察雅县| 大邑县| 红河县| 中方县| 黄浦区| 中西区| 济阳县| 桃园市| 贵德县| 徐汇区| 德江县| 名山县| 正蓝旗| 河池市| 灵武市| 安远县| 嘉定区| 射洪县| 梧州市| 舟曲县| 清流县| 金乡县| 当雄县| 鹿泉市| 布拖县| 台前县| 台东市| 常熟市| 赤水市|