Welcome to the code Farming Architecture technology sharing community

The core part of the producer-consumer pattern is a special container between producers and consumers, and blocking queues are the most common implementation of special containers. The JDK defines the BlockingQueue interface, which provides various implementations of blocking queues, including ArrayBlockingQueue, the main object of this section, The class in Java. Util. Concurrent. ArrayBlockingQueue. Java. The core methods that this class needs to implement are as follows. Let’s look at how ArrayBlockingQueue works in detail.

From the name can be seen that its storage structure is an array, that is, based on the array to achieve a FIFO blocking queue. New elements are inserted at the end of the queue, so that the first entry is at the head and the last entry is at the end of the queue. The array is bounded, so you need to specify the size of the array when constructing it. In addition, the blocking queue provides both fair and unfair modes.

Using the example

Before we look at how ArrayBlockingQueue works, let’s take a look at a few examples. In the first example, we created an ArrayBlockingQueue object, and then used five threads to produce data and put it to the blocking queue. The second main thread acts as a consumer, constantly pulling data from the blocking queue for consumption. The program output is as follows:

We create a block queue with a maximum length of 4, and then put one data into each of the five threads. However, since the maximum length of the block queue is 4, it will block after putting four data.

We create a block queue with a maximum length of 10. Then no producer produces data, but another consumer calls take to consume data. A block occurs because the blocking queue is empty.

Realize the principle of

Let’s start with the properties and constructors of the ArrayBlockingQueue class. Where the count variable represents the length of the queue, takeIndex and putIndex represent the index of queue entry and queue exit respectively, items is an Object array used to store the elements of the queue, lock is a concurrency control lock, NotEmpty and notFull represent conditions when the queue is non-empty and non-full, respectively. Provides two constructors, we specify the size of the queue when we create it, and a fair mode parameter, which is not fair by default. The lock object uses the ReentrantLock object, as does the fairness mechanism, which we have examined in depth in the previous section. The notEmpty and notFull condition objects are created from the lock object’s newCondition.

The PUT and take methods are the enqueue and dequeue methods that block queues and indirectly call enqueue and dequeue methods. The put method checks that the queued element cannot be null, obtains the lock before executing the enQueue method to maintain the array, and calls the await method of the notFull condition if the array has reached the maximum length. The take method obtains the lock before the dequeue method maintains the array, and if the array length is 0 calls the await method of the notEmpty condition to wait.

The Offer and poll methods are queue-in and queue-out methods that support timeout blocking queues, and you can see that the main difference is that they call the awaitNanos methods of the notFull and notEmpty conditions to wait, respectively. If the specified timeout period is exceeded, false is returned, indicating that joining the team failed due to timeout. If the timeout period exceeds the specified timeout period, the system returns NULL, indicating that the queue fails due to timeout.

conclusion

Around one of the implementations of BlockingQueue in the JDK, ArrayBlockingQueue. Its basic structure is to use arrays to hold blocking queue elements, which are limited in length and provide fair and unfair patterns. For the sake of convenience, we only keep the core source code of ArrayBlockingQueue. Only if we can understand the content thoroughly, we can already grasp the implementation principle of it. Recommended reading

Welcome to the code Farming Architecture technology sharing community