Creating a blocking Queue in .NET?

后端 未结 10 847
半阙折子戏
半阙折子戏 2020-11-22 01:14

I have a scenario where I have multiple threads adding to a queue and multiple threads reading from the same queue. If the queue reaches a specific size all threads<

10条回答
  •  攒了一身酷
    2020-11-22 01:51

    You can use the BlockingCollection and ConcurrentQueue in the System.Collections.Concurrent Namespace

     public class ProducerConsumerQueue : BlockingCollection
    {
        /// 
        /// Initializes a new instance of the ProducerConsumerQueue, Use Add and TryAdd for Enqueue and TryEnqueue and Take and TryTake for Dequeue and TryDequeue functionality
        /// 
        public ProducerConsumerQueue()  
            : base(new ConcurrentQueue())
        {
        }
    
      /// 
      /// Initializes a new instance of the ProducerConsumerQueue, Use Add and TryAdd for Enqueue and TryEnqueue and Take and TryTake for Dequeue and TryDequeue functionality
      /// 
      /// 
        public ProducerConsumerQueue(int maxSize)
            : base(new ConcurrentQueue(), maxSize)
        {
        }
    
    
    
    }
    

提交回复
热议问题