Reprint please indicate source: http://www.cnblogs.com/lighten/p/7491057.html
1. Preface
The concurrentlinkedqueue is an unbounded thread-safe queue that follows FIFO first-out principles. The head element is an element that has the longest time in the queue, that is, the insertion element is a trailing insert. The retrieval is started from the beginning element, and no empty elements are allowed. Iterators are weakly consistent and do not throw concurrency exceptions. Note: The size method is not a linear time operation, this is due to the characteristics of the queue, it is necessary to traverse the statistics, if the traversal process has been modified, you can get the wrong result (if not need a specific value, generally use isempty to judge, than size>0 faster). In addition, bulk operations, namely Addall,removeall,retainall,containsall and Equals,toarray, do not guarantee atomicity.
2.concurrentlinkedqueue2.1 Data structure
The data structure is very simple, there is only a head and a tail node type of the tail-ends, so its size method is traversed, because there is no lock, so the size may actually be incorrect.
Node nodes are also very simple, with only one value and a reference to the next node. However, the substitution is done through CAS operations.
The Kinsoku node is initialized to a node with a value of NULL.
2.2 Basic operations
Add a single element:
The first time the tail node is assigned to T,t is a snapshot of the current moment. Start with a single thread to understand, first insert an element 1, this time q==null, set P's next for element 1, note that at this time the tail node is not changed. Insert another element 2, because tail does not change, so p is not null,p = q = element 1 into the next loop, element 1 of the following node is null, so element 2 is set to the next of element 1, at this time p!=t, resets the end of the element. As can be seen from this process, tail is not the first time to change, but to change more than one. Multi-threaded in the case of queue jumping, was cut in the queue also need to determine whether the number of teams are inserted, the tail node is changed to be inserted two and changed, you need to use the latest tail node to try to insert the loop, otherwise select a later node. The p==q situation occurs when the poll operation: Assuming that there are only two nodes in the queue, but when the tail node is redefined by other threads, the head and tail are not changed at this time. A thread then inserts an element, at which point the t=tail,p=tail is paused when the Q is assigned, and it is understood that the head and tail are equal and are null nodes. The B thread executes the poll operation, poll the first head node element is null, not what we need, loop again, find element 1. When element 1 updates the head node, the head of the queue becomes element 2, but the head's next (null node) becomes the head itself. This time back to a thread, Tail=head, q=tail.next=tail=p, that is, the resulting p equals Q. If you do not understand the reference article: here.
Remove an element:
Here are some also described above. Poll setting the head node is also to skip one before setting the head node. Understanding this, this code is nothing, that is, through the CAs to ensure thread safety. Here P==q the situation is more simple, is the situation of offer, but at the same time there are two threads poll, B thread success, c thread failure will produce p==q situation.
Other methods are no longer described. When this collection is used, it is important to note that the use of improper efficiency will decrease. Give another reference article: here.
The Java Collection (17) Concurrentlinkedqueue