The tools for concurrent programming in Java have been introduced in previous articles: the BlockingQueue interface, ArrayBlockingQueue, and DelayQueue.
The LinkedBlockingQueue queue is the implementation class of the BlockingQueue interface, so it has all the features of the BlockingQueue interface. The LinkedBlockingQueue queue sorts elements in a first-in-first-out (FIFO) fashion. LinkeBlockingQueue provides two constructors, a constructor that builds a queue with a fixed number of queue sizes, and a no-argument constructor that builds a queue with integer.max_value.
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
Copy the code
Compare ArrayBlockingQueue with LinkedBlockingQueue
ArrayBlockingQueue and LinkedBlockingQueue are both implementations of the BlockingQueue interface, so they are used in the same way. We will not describe how to use them, but from the perspective of their performance and implementation of the underlying data structure
- ArrayBlockingQueue inserts and deletes data using a single lock lock, and reads and writes cannot be done in parallel. So execution is slower than LinkedBlockingQueue in high concurrency scenarios.
-
LinkedBlockingQueue uses a variant of the “Two Lock Queue” algorithm, ReentrantLock: takeLock, putLock, allow read and write parallelism, remove(e) and iterators need to acquire two locks. This reduces the likelihood that a thread will be stuck in a WAITING state because it cannot obtain a lock, thus improving the efficiency of concurrent execution.
-
The underlying code for ArrayBlockingQueue is implemented using arrays, which must be created to specify the queue’s capacity and allocate storage space. LinkedBlockingQueue is implemented using a linked list data structure. The storage space allocation of linked list nodes is dynamic. New element objects are added to the queue to allocate space, and the storage space is GC after the element objects are removed from the queue. However, using a LinkedBlockingQueue data structure is both a strength and a weakness, as high concurrency scenarios require frequent garbage collection by the Java JVM due to dynamic space allocation.
Overall, LinkedBlockingQueue performs better than ArrayBlockingQueue in concurrent scenarios. However, the preferred implementation of high-performance queues in Java is Disruptor, which does not come with the JDK. The underlying performance of Log4j2, which is well known to Java programmers, is significantly improved over LogBack and Log4J due to asynchronous logging using disruptor high-performance queues
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series