当前位置:网站首页>Introduction to the principles of linkedblockingqueue, arrayblockingqueue, synchronousqueue, concurrentlinkedqueue and transferqueue
Introduction to the principles of linkedblockingqueue, arrayblockingqueue, synchronousqueue, concurrentlinkedqueue and transferqueue
2022-06-11 08:16:00 【Thinking practitioner】
One 、jdk Characteristics of squadron
stay jdk There are many queues in the , It is difficult to use , Because it involves concepts such as concurrency . Now? , Let's list the characteristics of queues :
(1) Concurrent , There will be no thread safety issues .
(2) Queues have elements .
(3) All have added ( Producer side use )、 obtain ( Consumer side use ) function .
(4) In a multithreaded 、 Use in high concurrency scenarios .
Two 、jdk What are the squadrons
2.1、LinkedBlockingQueue
LinkedBlockingQueue Is an unbounded 、 Waiting queue with cache .
stay SingleThreadPool( The thread pool of a single thread )、FixedThreadPool( Thread pool with fixed number of threads ) The queues used in are LinkedBlockingQueue.
LinkedBlockingQueue It's a block queue based on a linked list , Internally, a data buffer queue is maintained ( The queue consists of a linked list ). When the producer puts a data into the queue , The queue gets the data from the producer , And buffered in the queue , And the producer immediately returns ; Only when the queue buffer reaches “ Maximum cache capacity ” when (LinkedBlockingQueue This value can be specified through the constructor ), Will block the producer queue , Until the consumer consumes a piece of data from the queue , Producer thread will be woken up . Treatment of consumers , Based on the same principle .
LinkedBlockingQueue The reason why it can handle concurrent data efficiently , It is because it adopts independent for the producer side and the consumer side ReentrantLock lock To control data synchronization , It also means that : In the case of high concurrency , Producers and consumers can manipulate the data in the queue in parallel , To improve the concurrency performance of the whole queue . these two items. ReentrantLock Lock as :takeLock and putLock, They are used when adding elements and when removing elements .
2.2、ArrayBlockingQueue
ArrayListBlockingQueue Is a bounded 、 Waiting queue with cache , Its add and get operations use the same ReentrantLock. In Alibaba's open source framework RocketMq in , Using the ArrayBlockingQueue queue .
ArrayBlockingQueue Is an array based blocking queue , Same as LinkedBlockingQueue similar , A fixed length data buffer queue is maintained internally ( The queue consists of arrays ).ArrayBlockingQueue Two shaping variables are also stored inside , Mark separately “ The position of the head and tail of the queue in the array ”.
ArrayBlockingQueue When producers put in data and consumers get data , All share the same lock object , It also means that they can't really run in parallel . This is particularly different from LinkedBlockingQueue. Analyze according to the principle of implementation ,ArrayBlockingQueue Detachable lock is fully available , In order to realize the complete parallel operation of producer and consumer operation .Doug Lea Why not , Maybe it's because ArrayBlockingQueue Data write and get operations are light enough , So as to introduce independent locking mechanism , In addition to the extra complexity of the code , There is absolutely no benefit in performance . ArrayBlockingQueue and LinkedBlockingQueue Another obvious difference is that : When inserting or deleting elements , The former does not generate or destroy any additional object instances , The latter will generate an additional Node object . In a system that requires efficient and concurrent processing of large amounts of data over a long period of time , about GC There are still some differences in the impact of .
2.3、SynchronousQueue
SynchronousQueue Is an unbounded 、 Unbuffered waiting queue , There can only be one element at most . stay CachedThreadPool In the thread pool , Using the SynchronousQueue.
because SynchronousQueue Its own characteristics , After an element is added , You must wait for other threads to take it away , To continue adding . It can be said that ,SynchronousQueue Is a cache with a length of 1 Blocking queue . however , Its isEmpty() Method always returns true,remainingCapacity() Method always returns 0,remove() and removeAll() Method always returns false,iterator() Method always returns null ,peek() Method always returns null.
Make a statement SynchronousQueue There are two different ways : Fair model and unfair model . If fair mode is adopted , that SynchronousQueue Will be used Fair lock , Use TransferQueue fifo The way , To block redundant producers and consumers , So as to make the system as a whole Fair . If an unfair model is adopted (SynchronousQueue Default ), that SynchronousQueue use Not fair lock , Use TransferStack Last in, first out The way , To manage redundant producers and consumers . For the latter mode , If there is a gap between the processing speed of producers and consumers , Is prone to hunger and thirst , That is, there may be some producers or consumers whose data will never be processed . Locking is the use of CAS Way to replace .
SynchronousQueue The addition and acquisition methods of both use transfer Method .
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
return transferer.transfer(e, true, 0) != null;
}public E take() throws InterruptedException {
E e = transferer.transfer(null, false, 0);
if (e != null)
return e;
Thread.interrupted();
throw new InterruptedException();
}
2.4、ConcurrentLinkedQueue
ConcurrentLinkedQueue Is a link node based 、 unbounded 、 Non blocking 、 Thread safe queues .
stay netty The read byteBuffer And access Selector in , It's all used ConcurrentLinkedQueue queue .
ConcurrentLinkedQueue Use the first in first out rule to sort nodes , from tail Node add , from head Node acquisition , And use “wait-free” The algorithm implements high concurrency support . Because there is no lock , So it's more efficient .
Specific and detailed principle , You can search on the Internet , No more details here .
2.5、LinkedTransferQueue
LinkedTransferQueue Is a linked list structure 、 unbounded 、 Blocking TransferQueue queue . Compared with other blocking queues ,LinkedTransferQueue More tryTransfer and transfer Method .
LinkedTransferQueue yes ConcurrentLinkedQueue、SynchronousQueue( Transfer elements in fair mode )、LinkedBlockingQueue( Blocking Queue Basic approach ) Superset . and LinkedTransferQueue Better to use , Because it not only integrates the functions of these classes , It also provides a more efficient implementation , Its addition and acquisition are used xfer() Method .
边栏推荐
- Selenium click the floating menu and realize the functions of right mouse button
- DAMENG 数据库启停
- DAMENG 数据库登陆
- torch. unbind()
- (resolved) pychart debug error -unicode decodeerror: 'UTF-8' codec can't decode byte 0xe8 in position 1023
- Method summary of creating deep learning model with keras/tensorflow 2.9
- TypeScript-接口和类型别名异同
- SOCKET【5】- struct linger 用法
- Typescript declaration merge
- torch. nn. functional. pad
猜你喜欢

node报错整理

Selenium click the floating menu and realize the functions of right mouse button

如何开始参与开源社区

Process control: process waiting (recycling child processes)

安卓初中级开发基础知识整理(面试自用)

Batch splice string

Bladed入門教程(視頻)

Anaconda+tensorflow most effective summary version (blood and tears summary of 6 reloads)

torch. meshgrid

Jupyter notebook code completion plug-in + Solution
随机推荐
TypeScript-枚举
Printing diamond of beginner C
C. Managing history (greedy / hashing / thinking / good questions)
(completely solved) dataframe assignment settingwithcopywarning: a value is trying to be set on a copy of a slice
Data visualization and Matplotlib
(the slow download speed of cifar10 in torchvision has been solved) how to download and use torchvision import
Xshell7 and xftp7 to continue using this program, you must apply the latest updates or use a new version
Typescript type alias
[transfer] two-way merging and sorting of C language
项目实训-克隆门
Dameng user management
Jupyter notebook code completion plug-in + Solution
Js基础学习Script
How CSDN reports plagiarized articles
Typescript type protection
DAMENG 数据库启停
Introduction to guava cache usage
Modulenotfounderror: no module named 'tensorboard in pytorch‘
Note: JDBC
自定义ViewGroup的知识点总结-持续更新