Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does LinkedTransferQueue understand the synthesis of blocking queues

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to understand the complex LinkedTransferQueue of the blocking queue, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

In some implementations, LinkedTransferQueue can be regarded as a superset of ConcurrentLinkedQueue, SynchronousQueue, and LinkedBlockingQueue, and can be used as a comparative learning.

Introduction to LinkedTransferQueue

LinkedTransferQueue is an unbounded blocking queue composed of linked list structures that implements the TransferQueue interface. The TransferQueue interface inherits BlockingQueue and mainly extends two methods: tryTransfer and transfer.

The put method of BlockingQueue can succeed directly when the queue is not full and will not block the thread, while the transfer method of the TransferQueue extension will block until the added data is consumed by consumers, which is very similar to the put method of SynchronousQueue learned in the previous article, so LinkedTransferQueue has the function of SynchronousQueue.

Basic introduction

The construction method of LinkedTransferQueue is relatively simple, a no-parameter construction method, and a construction method that accepts a set, which is to put the data in the collection into the queue. Nothing else has been initialized.

Like the previous blocking queue, it inherits AbstractQueue, so there are some of the same methods, put, offer, add methods to add data to the queue, because the queue is an unbounded queue, so these methods must succeed without blocking. While the take and poll methods consume data in the queue, the take method may block, and poll has two methods that can return directly or delay waiting for a period of time.

The underlying linked list structure of the queue is an inner class Node. The main properties are as follows:

Boolean isData: node true created by the method of adding data, and consumption is false

Object item:item indicates the enrollment data, and the consumption method is null

Node next: next node

Thread waiter: blocked thread

By looking at the source code, we find that put, offer, add, take, poll methods, including tryTransfer and transfer, are all called xfer methods, so we focus on the xfer method.

Xfer method implementation

Explain the method parameters before viewing the source code. The method "private E xfer (E, boolean haveData, int how, long nanos)" is explained as follows:

E represents the data to be added, and take and poll are null

HaveData indicates whether there is any data. The method of adding class is true, and the consumer category is false.

How represents method blocking mode. LinkedTransferQueue defines four static variables NOW, ASYNC, SYNC and TIMED,NOW to indicate that no blocking is used in poll and tryTransfer methods, ASYNC is used in put, offer and add methods, SYNC means blocking is used in take methods, TIMED is used in poll and tryTransfer delay methods

Nanos indicates the maximum blocking time, which is used by the poll and tryTransfer methods

After understanding the method parameters, take a look at the source code parsing directly, as shown below:

The main process is divided into two steps, the first is to match different nodes from the existing linked list, and then carry on the subsequent processing after all the nodes are traversed without matching, and then judge whether the thread is blocked according to the passed how parameters.

You can see that NOW will directly return null (for non-blocking poll, tryTransfer), ASYNC will add nodes to the linked list and return (for queuing methods), while others will call the awaitMatch method to wait for wake up and return the result.

The code looks complex, but the main process is actually relatively simple. Here is a brief summary of the main process as shown in the following figure:

LinkedTransferQueue does not block producers because it is an unbounded queue. It provides the functionality provided by LinkedBlockingQueue, but it has one more transfer function.

Similar to the fair lock implementation of SynchronousQueue, but the producers of LinkedTransferQueue will not block, and SynchronousQueue will be consumed by both consumers and producers in order to continue, that is, the focus is on synchronization.

But both LinkedTransferQueue and SynchronousQueue are implemented through CAS and loops, while LinkedBlockingQueue is implemented through locks.

The above is how to understand LinkedTransferQueue, the synthesis of blocking queues. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report