Java thread pool queue LinkedTransferQueue

public enum QueueTypeEnum {
    ARRAY_BLOCKING_QUEUE(1, "ArrayBlockingQueue"),
    LINKED_BLOCKING_QUEUE(2, "LinkedBlockingQueue"),
    DELAY_QUEUE(3, "DelayQueue"),
    PRIORITY_BLOCKING_QUEUE(4, "PriorityBlockingQueue"),
    SYNCHRONOUS_QUEUE(5, "SynchronousQueue"),
    LINKED_TRANSFER_QUEUE(6, "LinkedTransferQueue"),
    LINKED_BLOCKING_DEQUE(7, "LinkedBlockingDeque"),
    VARIABLE_LINKED_BLOCKING_QUEUE(8, "VariableLinkedBlockingQueue"),
    MEMORY_SAFE_LINKED_BLOCKING_QUEUE(9, "MemorySafeLinkedBlockingQueue");
}

LinkedTransferQueue

Unbounded blocking queue LinkedTransferQueue, which is also implemented based on linked list, is first-in first-out for all given elements. LinkedTransferQueue can be regarded as a combination of LinkedBolckingQueue and SynchronousQueue. SynchronousQueue cannot store elements inside, and needs to be blocked when elements are to be added. LinkedBolckingQueue uses a lot of locks internally, and its performance has declined.

public class LinkedTransferQueue<E> extends AbstractQueue<E>
    implements TransferQueue<E>, java.io.Serializable {
    
    

Compared with other blocking queues, LinkedTransferQueue has more tryTransfer(E e) and transfer(E e) methods.

public interface TransferQueue<E> extends BlockingQueue<E> {
    
    
    // 如果可能,立即将元素转移给等待的消费者。 
    // 更确切地说,如果存在消费者已经等待接收它(在 take 或 timed poll(long,TimeUnit)poll)中,则立即传送指定的元素,否则返回 false。
    boolean tryTransfer(E e);

    // 将元素转移给消费者,如果需要的话等待。 
    // 更准确地说,如果存在一个消费者已经等待接收它(在 take 或timed poll(long,TimeUnit)poll)中,则立即传送指定的元素,否则等待直到元素由消费者接收。
    void transfer(E e) throws InterruptedException;

    // 上面方法的基础上设置超时时间
    boolean tryTransfer(E e, long timeout, TimeUnit unit) throws InterruptedException;

    // 如果至少有一位消费者在等待,则返回 true
    boolean hasWaitingConsumer();

    // 返回等待消费者人数的估计值
    int getWaitingConsumerCount();
}

When the LinkedTransferQueue consumer thread obtains data: call methods such as take poll

If the queue is not empty, the data will be taken directly. If the queue is empty, the consumer thread will generate a placeholder virtual node (the node element is null) to join the queue, and wait on this node, and then the producer thread will request to add data , it will start traversing from the head node of the one-way linked list. If a node is found to be a node of the fetch request task type (that is, the isData of this node is false, item == null), the producer thread will not join the queue , directly fill the element into the node (the element is passed to it), and wake up the consumer thread waiting for the node, and the awakened consumer thread takes the element.

image.png

tryTransfer(E e) method

tryTransfer(E e) When the producer thread calls the tryTransfer method, if there are no consumers waiting to receive elements, it will return false immediately. The difference between this method and the transfer method is that the tryTransfer method returns immediately regardless of whether the consumer receives it, while the transfer method must wait for the consumer to consume before returning.

tryTransfer(E e, long timeout, TimeUnit unit) adds a time-limited waiting function. If there is no consumer to consume the element, it will wait for the specified time before returning; if the element has not been consumed by the timeout, it will return false. If it is within the timeout Returns true if the element is consumed.

transfer(E e) method

The transfer method is used to transfer the specified element e to the consumer thread (calling the take/poll method). If a consumer thread is blocking and waiting, the thread calling the transfer method will directly pass the element to it; if there is no consumer thread waiting to get the element, the thread calling the transfer method will insert the element to the end of the queue, and then block waiting, Until a consumer thread appears to get the element.

Guess you like

Origin blog.csdn.net/qq_34626094/article/details/130517257