Concurrentlinkedqueue class in Java

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/u010142437/article/details/22734857

First, class structure:

Java.lang.Object  java.util.abstractcollection<e>      java.util.abstractqueue<e>          Java.util.concurrent.concurrentlinkedqueue<e>

Type parameter: E-the element type maintained in this collection all implemented interfaces: Serializable, Iterable<e>, Collection<e>, queue<e>
Ii. Overview:

abstractqueueQueueSerializable

A node-based, link-free, thread-safe queue. This queue sorts the elements according to the FIFO (first-in, in-out) principle. The head of the queue is the longest element in the queue. The tail of the queue is the element with the shortest time in the queue. The new element is inserted at the end of the queue, and the queue get operation obtains the element from the queue head. Concurrentlinkedqueue is an appropriate choice when multiple threads share access to a common collection. This queue does not allow the use of null elements.

This implementation employs an efficient no-wait (wait-free) algorithm based on Simple, Fast, and practical non-blocking and Bl, co-authored by Maged M. Michael and Michael L. Scott. Ocking the algorithm described in Concurrent Queue algorithms.

Be careful, unlike most collection, thesize method is not a fixed-time operation. Because of the asynchronous nature of these queues, determining the number of current elements requires traversing those elements.

This class and its iterators implement all the Collection optional Iterator methods of the and interface.

Memory consistency Effect: when there are other concurrent collection, the action of putting the object ConcurrentLinkedQueue in the previous thread Happen-before subsequently ConcurrentLinkedQueue accesses or removes the element from the operation by another path.

This class is a member of the Java collections Framework.

Third, the construction method: 1 , public concurrentlinkedqueue() creates an initially empty concurrentlinkedqueue.

2. Public Concurrentlinkedqueue (Collection<? extends E> C)

creates a collection that initially contains the given element. Concurrentlinkedqueue , add elements according to the traversal order of this collection iterator.

Parameters:
c-Elements originally contained collection
Thrown:
NullPointerException-If the specified collection or any of its elements is null
Iv. method Details:1,add(e  )     inserts the specified element into the tail of this queue. 

designated by: Interface Collection<E> in theadd

designated by: Interface Queue<E> in theadd、

Coverage: class AbstractQueue<E> in theadd

Parameters: o -the element to be added

return: true (according to Collection.add(E) the Provisions)

Thrown: NullPointerException -If the specified element is null

2. Public Boolean Offer (e ) inserts the specified element into the tail of this queue.

designated by: Interface Queue<E> in theoffer

Parameters: e -the element to be added

return: true (according to Queue.offer(E) the Provisions)

Thrown: NullPointerException -If the specified element is null

3. Public E Description of poll() Queue Copy from Interface : gets and removes the header of this queue and returns if this queue is empty NULL .

designated by: Interface Queue<E> in thepoll

return: The header of the queue, if this queue is empty, returns NULL

4. The public E - Peek() Queue copy of the description from the interface :  Gets the header of this queue without removing it, or null if this queue is empty .

designated by: Interface Queue<E> in thepeek

return: The header of this queue, or if this queue is empty, returns NULL

5, Publicboolean isEmpty() returns True if this queue does not contain any elements .

designated by: Interface Collection<E> in theisEmpty

Coverage: class AbstractCollection<E> in theisEmpty

return: If this queue does not contain any elements, it returns true

6, publicint size() Returns the number of elements in this queue. If this queue contains more elements than integer.max_value, Integer.max_value is returned .

It is necessary to be careful that, unlike most collection, this method is not a fixed-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires an O (n) time traversal to be taken.

designated by: Interface Collection<E> in thesize

designated by: class AbstractCollection<E> in thesize

return: The number of elements in this queue

7. Public Boolean contains (Object o) If this queue contains the specified element, the return true . More precisely, returns True when and only if this queue contains at least one element e that satisfies o.equals (e) .

designated by: Interface Collection<E> in thecontains

Coverage: class AbstractCollection<E> in thecontains

Parameters: o -To check if an object is contained in this queue

return: If this queue contains the specified element, the return true

8,public boolean remove(Object o) Removes a single instance of the specified element from the queue, if one exists. More specifically, if this queue contains one or more element ethat satisfies o.equals (e) , then one such element is removed. Returns true if this queue contains the specified element (or if the queue has been changed due to a call) .

designated by: Interface Collection<E> in theremove

Coverage: class AbstractCollection<E> in theremove

Parameters: o -the element to be removed from this queue (if present)

return: If this queue changes because of a call, it returns true

9, public Object[] toArray() returns an array containing all elements of this queue in the proper order.

Because this queue does not maintain any references to the returned array, it will be "safe." (In other words, this method must be assigned a new array). Therefore, the caller can arbitrarily modify the returned array.

This method serves as a bridge between an array-based API and an collection-based API.

designated by: Interface Collection<E> in thetoArray

Coverage: class AbstractCollection<E> in thetoArray

return: an array containing all the elements of this queue

10. Public <T> t[] ToArray (t[] a) returns an array that contains all the elements of this queue in the proper order; The run-time type of the returned array is the run-time type of the specified array. If the specified array can hold the queue, the queue is returned here. Otherwise, a new array with the run-time type of the specified array and the size of this queue is assigned.

if the specified array can hold the queue and has the remaining space (that is, the array has more elements than the queue), then the element immediately following the tail of the queue is set to NULL .

like toArray() method, this method acts as a bridge between an array-based API and an collection-based API. Further, this approach allows for precise control of the run-time type of the output array, which in some cases can be used to save allocation overhead.

assume that x is a well-known queue that contains only strings. The following code is used to dump the queue to a newly allocated String array:

string[] y = X.toarray (new string[0]);

Note that ToArray (new object[0]) and the ToArray () are functionally the same.

designated by: Interface Collection<E> in thetoArray

Coverage: class AbstractCollection<E> in thetoArray

Parameters: a -an array that will be used to store the queue elements (if the array is large enough), otherwise a new array with the same run-time type will be assigned to this

return: an array containing all the elements of this queue

Thrown: ArrayStoreException -If the run-time type of the specified array is not a super-type of run-time type for each element in this queue

NullPointerException-If the specified array is null

11. Public Iterator < E > iterator () returns an iterator that iterates in the appropriate order on this queue element. The returned iterator is a "weakly consistent" iterator that does not throw and ConcurrentModificationException guarantees the elements that exist when the iterator is constructed, and may (but is not guaranteed) reflect any modifications that have been constructed.

designated by: Interface Iterable<E> in theiterator

designated by: Interface Collection<E> in theiterator

designated by: class AbstractCollection<E> in theiterator

return: Iterators that iterate in the proper order on this queue element

Concurrentlinkedqueue class in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.