How to add and fetch data in ArrayBlockingQueue of JUC
This article mainly explains "how to add and fetch data in JUC ArrayBlockingQueue". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to add and fetch data with JUC ArrayBlockingQueue".
ArrayBlockingQueue has the following characteristics:
A bounded blocking queue implemented by an array. Once the capacity is created, the subsequent size cannot be modified.
In accordance with the first-in-first-out rule, the head of the team takes the data and the end of the team takes the data.
Like LinkedBlockingQueue, when the queue is full, the put data in the queue will be blocked. When the queue is empty, getting data to the queue will be blocked.
When you operate on data, you share a lock, so you can't read and write at the same time.
ArrayBlockingQueue, like LinkedBlockingQueue, also inherits AbstractQueue to implement the BlockingQueue interface, so it is the same as LinkedBlockingQueue in terms of methods, so instead of listing the methods here, we can check out the previous LinkedBlockingQueue article ~
In addition to methods, there are two more important parameters in ArrayBlockingQueue:
/ * * items index for next take, poll, peek or remove * /
/ / get the location of the element
Int takeIndex
/ * * items index for next put, offer, or add * /
/ / Array subscript when adding elements
Int putIndex
Since the bottom layer of ArrayBlockingQueue is an array, combined with the above two parameters, the overall structure of ArrayBlockingQueue is roughly as follows:
ArrayBlockingQueue has three constructors:
Public ArrayBlockingQueue (int capacity)
/ / fair indicates whether it is a fair lock
Public ArrayBlockingQueue (int capacity, boolean fair)
Public ArrayBlockingQueue (int capacity, boolean fair, Collection