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--
This article introduces you how to block queue LinkedBlockingQueue source code learning and comparison, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Learn the LinkedBlockingQueue source code today and compare it with it.
LinkedBlockingQueue summary
Similarly, we first directly summarize the features related to LinkedBlockingQueue, and then explain it according to the source code. Its main features are as follows:
1. He uses linked list to realize data storage at the bottom.
2. He is a FIFO unbounded blocking queue. The maximum length of data defaults to the maximum value Integer.MAX_VALUE of Intenger, and the length can also be set.
Introduction to important attributes
The main attributes are shown below:
LinkedBlockingQueue has an inner class Node, which is used to form its linked list of stored data, the item data of Node is used to store data, next represents the next node, and the next of the tail node is null.
Capacity capacity represents the maximum amount of data that can be stored in the queue. It is set during initialization. The default is Integer.MAX_VALUE.
Count represents the amount of data currently stored, which is of type AtomicInteger (can you imagine why? )
The header node of the head linked list, whose item does not store data
The tail node of the tail linked list, whose next is null, that is, there is no next node
Blocking of take methods controlled by takeLock and notEmpty
Blocking of put methods controlled by putLock and notFull
You can guess from the above attributes that LinkedBlockingQueue uses two locks to control data reading and writing, which is equivalent to separating read and write, and can exist read and write operations at the same time, so count may have multiple thread modifications at the same time, so use AtomicInteger because of thread safety problems.
The two most critical proprietary methods
Similarly, LinkedBlockingQueue has two same key methods. The specific code and explanation are shown in the following figure:
LinkedBlockingQueue's enqueue and dequeue methods are simpler than ArrayBlockingQueue's.
The enqueue method is to put the new node into the next of last, and then set the node to the tail node.
Dequeue first takes the header node next node as the first node, and then the next of the previous header node is set to itself, and the GC is removed from the linked list. Next time, the header node will be recycled.
Then first is used as a head, and the item is taken out and set to null, so the header node does not store data and is upgraded by the next node.
These two methods of LinkedBlockingQueue are relatively simple, but it can also be seen that it implements FIFO FIFO.
Introduction of main methods
LinkedBlockingQueue, like ArrayBlockingQueue, inherits from AbstractQueue and implements the BlockingQueue interface, so the methods they provide are similar, the functions are similar, and the main implementations are different. As discussed in detail in the previous article, let's just look at the implementation of take and put methods.
The source code of the put method is shown below:
There are four steps:
Encapsulate the data into Node nodes to acquire put locks
Verify that the queue is full, which blocks the thread
If it is not full, add the node to the queue, count plus 1, and get the number before adding.
Wake up blocked put threads if the current number is still less than the maximum capacity, and wake up take threads if the previous number is 0
The source code of the take method is shown below:
The take method is similar to the put method, which is divided into the following four steps:
First acquire the take lock
Determine whether there is data in the queue, and if not, block the thread
Call the dequeue method to get the data and subtract the count by 1
If the number of queues before getting is greater than 1, there is still data, and you can wake up other take threads. If the number of queues before getting is equal to the maximum number of containers, it means that there may be blocked put threads that need to be woken up.
You can see that the put and take methods are very simple. They both acquire the lock for the first time, then judge whether it is blocked according to the number of queues, then add or obtain data, and finally wake up the corresponding thread.
Compare with ArrayBlockingQueue (key point)
LinkedBlockingQueue and ArrayBlockingQueue are very similar classes, and their advantages and disadvantages can be better reflected by comparison. The main comparison is as follows:
Compared with the underlying properties, the underlying implementation of LinkedBlockingQueue is a linked list, and ArrayBlockingQueue is an array. ArrayBlockingQueue initialization requires an array, while LinkedBlockingQueue is a dynamic number of Node objects; so ArrayBlockingQueue needs to pre-allocate memory, while LinkedBlockingQueue does not, and if you expect a large amount of data to be cached, ArrayBlockingQueue requires a large chunk of data from the start.
But ArrayBlockingQueue is faster to add elements, because it is just a reference to the object to be saved in the corresponding location in the array, and LinkedBlockingQueue needs to create a Node object; at the same time, after getting it, the Node object becomes garbage, which will add a lot of garbage in the case of large read and write, which may affect the performance of the program.
ArrayBlockingQueue uses one lock to control read and write, and LinkedBlockingQueue uses two locks to control read and write. Because ArrayBlockingQueue does not have to worry about data overwriting and LinkedBlockingQueue can support reading and writing at the same time, ArrayBlockingQueue supports one operation at the same time, so LinkedBlockingQueue throughput is higher.
Therefore, ArrayBlockingQueue takes up fixed memory, so it cannot support too much cache, but the latency of each operation is lower, and LinkedBlockingQueue does not need to use an initialized memory temporarily, but each operation consumes more memory, but it also supports reading and writing, so the throughput is higher.
However, when using LinkedBlockingQueue, there may be a memory overflow if the default size is used and when the production speed is faster than the consumption speed.
LinkedBlockingQueue and ArrayBlockingQueue provide exactly the same functions, but their underlying implementations are different, so they have different priorities and can be chosen on a case-by-case basis.
On how to carry out blocking queue LinkedBlockingQueue source code learning and comparison is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.