In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to analyze the blocking queue ArrayBlockingQueue source code, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
ArrayBlockingQueue summary
First, summarize the features related to ArrayBlockingQueue directly and then explain it according to the source code. Its main features are as follows:
1. It is a FIFO bounded blocking queue implemented by an array, which is decorated by final.
2. ArrayBlockingQueue is bounded and fixed, and the size must be specified when constructing the function. It cannot be changed after confirmation (the array is determined and immutable).
3. "fairness" is not guaranteed in multithreaded environment.
4. Realize thread safety through ReentrantLock and Condition.
Introduction to important attributes
The main attributes are shown below:
The properties of ArrayBlockingQueue are relatively simple, starting with the Object array that holds the data, which is decorated with final, so the length of the queue is immutable.
Then the three int attributes indicate where you should get the data from the array the next time you get the data, where you should save the data to the array the next time you save the data, and count records how many more are available.
All methods must acquire lock locks first. Lock has fair and unfair locks. Unfair locks are implemented by default, and can also be specified in initialization ArrayBlockingQueue, so the default ArrayBlockingQueue does not guarantee fairness.
Both notEmpty and notFull are created through lock, and both are initialized when ArrayBlockingQueue is initialized.
Here is a brief introduction to the role of attributes, and then understand its role through the source code.
The two most critical proprietary methods
First of all, let's talk about the two private methods. The main methods of the queue should finally rely on these two private methods. Look directly at the source code as shown below:
The enqueue method is used to save the data to the array items, which increments the putIndex, which is where it should be saved next time. If the putIndex equals the length of the array, it will be 0 next time.
In the end, threads that are blocked by calling notEmpty.await () will be awakened, and only the take method will actually be called.
The dequeue method is to get the data, which is the data at takeIndex. After getting it, the items [takeIndex] is set to null. Similarly, if the length of the array is equal to the length of the array, it will be set to 0 after increasing takeIndex.
Finally, threads that are blocked by calling notFull.await () will be awakened, and only the put method will be called.
So the main changes are putIndex and takeIndex, which are summarized here as follows:
If they are not protected in more than these relationships, for example, takeIndex runs faster than putIndex, then you will get null values and get one more cycle of putIndex than takeIndex, then data will be overwritten, resulting in data loss.
Only by ensuring that they are these relationships can we ensure the security of the data, prevent the data from being overwritten or get null values, and achieve FIFO first-in-first-out, and the public methods provided by queues must ensure this security.
Introduction to the method of adding data
ArrayBlockingQueue provides four add methods: add, offer, offer (specify blocking time), and put
The add method calls the offer method, while the offer method first acquires the lock, then determines whether the queue is full, and directly returns false if it is full, and calls enqueue to save the data if it is not full. So add and offer are not blocking.
Offer also has a method to block a specified time (timeout) when trying to save when the queue is full. When it is judged that the queue is full, it will call "nanos = notFull.awaitNanos (nanos);" block the thread, and return false if it is full after a certain period of time.
Put acquires the lock. If the array is full, call notFull.await to block and wait for wake-up. After waking up, verify whether it is full. If it is not full, call enqueue, otherwise block again. So adding data to the queue only the put method supports real blocking to ensure that the addition is successful, and other methods may fail to save.
Introduction to the method of obtaining data
Poll, poll (specified blocking time), take, peek are provided to obtain data.
The poll method acquires the lock first, and returns null if count==0, otherwise the dequeue method is called to get the result.
Poll (specified blocking time) blocks the execution time when judging count==0, then determines again, returns null if it is still equal to 0, and calls the dequeue method to get the result if it is not equal to 0.
The take method first acquires the lock and then calls notEmpty.await () to block if count is equal to 0 in a loop. Otherwise, dequeue is called to get the result. Similarly, the take method will guarantee that the data will be obtained, otherwise it will block all the time.
Peek means peeking. After acquiring the lock, the peek method directly gets the element return of the item [item Index], and then does nothing, as if peeking to see which element it will get next time, but does not affect the queue.
After reading the above, have you mastered how to analyze the blocking queue ArrayBlockingQueue source code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.