A deep understanding of blocking queues and a deep understanding of queues

Source: Internet
Author: User

A deep understanding of blocking queues and a deep understanding of queues

BlockingQueue is a queue that supports two additional operations. The two additional operations are as follows: when the queue is empty, the thread that obtains the element will wait for the queue to become non-empty. When the queue is full, the thread storing elements will wait for the queue to be available. Blocking queues are often used in scenarios where producers and consumers add elements to queues, while consumers take elements from queues. The blocking queue is the container where the producer stores elements, and the consumer only takes elements from the container.

First put a picture:


Based on the previous descriptions, we will consider the problems that will occur in the program when blocking the queue:
Two functions are required to block a queue: waiting for a thread and awakening a thread. The details are as follows:
In extreme conditions, a thread needs to be suspended. Wait until the queue meets the conditions and then add or extract the queue.
After the conditions of the queue are met, the notification thread will continue the operation before it suspends ....
Technologies involved:
Thread Synchronization and inter-thread Communication
Possible deadlock analysis:
At a certain time point, the queue is empty or full. At this time, the producer fails to store data or is still storing data in the queue. This will cause an error in the queue.
In this case, the consumer will encounter deadlocks when performing operations on the queue... because the previous operations of the producer have caused the queue to list problems and do not release the lock, this will cause deadlocks.
This is to solve the deadlock problem from the perspective of preventing deadlocks.
The first is synchronization resource-queue lock. Since there is a lock, we need to consider the deadlock problem, and finally the communication between threads.

That is to say, to implement blocking queues, consider these three points.

I checked the information. Most of them are java encapsulated class libraries, but it's okay. The ideas and theories are the same. The difference is that the implementations are different. But there is still a good C # implementation ---- So how can we implement blocking queues by ourselves? As mentioned above, synchronization and thread communication prevent deadlocks. Look at the Code:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading; using System. threading. tasks; namespace SuiBao. utility {// BlockingQueue is a queue that supports two additional operations. The two additional operations are as follows: when the queue is empty, the thread that obtains the element will wait for the queue to become non-empty. When the queue is full, the thread storing elements will wait for the queue to be available. Blocking queues are often used in scenarios where producers and consumers add elements to queues, while consumers take elements from queues. The blocking queue is the container where the producer stores elements, and the consumer only takes elements from the container. // Two functions are required for blocking the queue: waiting for the thread and awakening the thread. the details are as follows: // in extreme conditions, a thread needs to be suspended. Wait until the queue meets the conditions and then perform the Add or extract operation. // After the queue meets the conditions, the operation before the notification thread continues its suspension .... // involved technology: // thread synchronization (lock used in this instance) communication with threads (event used in this example) /// possible deadlock analysis: // at a certain time point, the queue is empty or full. At this time, the producer fails to store data or is still storing data in the queue. This will cause an error in the queue. // if at this time, A deadlock occurs when the consumer operates the queue... because of the previous operations by the producer, the team listed the problems and did not release the lock, this will cause a deadlock // This is to solve the deadlock problem from the perspective of Deadlock Prevention public class BlockQueue <T> {private Queue <T> _ inner_queue = null; private M AnualResetEvent _ dequeue_wait = null; public int Count {get {return _ inner_queue.Count ;}} public BlockQueue (int capacity =-1) {this. _ inner_queue = capacity =-1? New Queue <T> (): new Queue <T> (capacity); this. _ dequeue_wait = new ManualResetEvent (false);} // lock public void EnQueue (T item) {if (this. _ IsShutdown = true) throw new InvalidOperationException ("the service is not enabled. [EnQueue] "); lock (this. _ inner_queue) {this. _ inner_queue.Enqueue (item); this. _ dequeue_wait.Set () ;}// the public T DeQueue (int waitTime) {bool _ queueEmpty = false; T item = default (T); while (True) {lock (this. _ inner_queue) {// determine whether an element exists in the queue .... if (this. _ inner_queue.Count> 0) {item = this. _ inner_queue.Dequeue (); this. _ dequeue_wait.Reset (); // break;} else {if (this. _ IsShutdown = true) {throw new InvalidOperationException ("the service has not enabled [DeQueue]. ");} else {_ queueEmpty = true ;}}} if (item! = Null) {return item;} if (_ queueEmpty) {this. _ dequeue_wait.WaitOne (waitTime) ;}} private bool _ IsShutdown = false; public void Shutdown () {this. _ IsShutdown = true; this. _ dequeue_wait.Set ();} public void Clear () {this. _ inner_queue.Clear ();}}}

Is there any encapsulated blocking queue in. net? Yes! BlockingCollection <> class. In fact, many articles I have written about threads have mentioned this class library, which is also used in many places. The default container for this class is ConcurrentQueue. Therefore, synchronization is done, and the class also implements the blocking function:
Multiple Threads or tasks can add items to the set at the same time. If the set reaches its specified maximum capacity, the manufacturing thread will be blocked until an item in the set is removed. Multiple users can remove items at the same time. If the set is empty, the use of threads will be blocked until the producer adds an item. The manufacturing thread can use CompleteAdding to indicate that no items are added. The user will monitor the IsCompleted attribute to know when the set is empty and no more items are added.
Overview of BlockingCollection in https://docs.microsoft.com/zh-cn/dotnet/standard/collections/thread-safe/blockingcollection-overview)
I'm so sorry, Microsoft has a lot of good stuff. Why can't it be easily discovered and used like java?
Yes, we can use this class to easily implement blocking queues, and it is a perfect implementation.

For more blogs about multithreading and concurrency, please visit my blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.