当前位置:网站首页>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 .

原网站

版权声明
本文为[Thinking practitioner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020513584429.html