Java Concurrency-concurrent Collections

Source: Internet
Author: User
Tags int size

Data structures is a basic element in programming. Almost every program uses one or more types of data structures to store and manage their data. Java API provides the Java collections framework that contains interfaces, classes, and algorithms, which implement a lot of different data structures that's can use in your programs.

When you need to work with data collections in a concurrent program, you must is very careful with the implementation Choose. Most collection classes is not ready for work with concurrent applications because they don ' t control the concurrent a Ccess to its data. If some concurrent tasks share a data structure that's not ready for work with concurrent tasks, you can have data inconsi Stency errors that would affect the correct operation of the program. One example of this kind of data structures is the ArrayList class.

Java provides data collections that's can use in your concurrent programs without any problems or inconsistency. Basically, Java provides, kinds of collections to use in concurrent applications:

    • Blocking Collections: This kind of collection includes operations to add and remove data. If The operation can ' t be made immediately, because the collection was full or empty and the thread that makes the call WI ll be blocked until the operation can be made.
    • non-blocking Collections: This kind of collection also includes operations to add and remove data. If The operation can ' t be made immediately, the operation returns a null value or throws an exception, but the thread That makes the call won ' t be blocked.

Concurrentlinkeddeque

An unbounded concurrent deque based on linked nodes. Concurrent insertion, removal, and access operations execute safely across multiple threads. A Concurrentlinkeddeque is an appropriate choice when many threads would share access to a common collection. Like most other concurrent collection implementations, this class does not permit the use of NULL elements.

See more ...

Method Summary

public void AddFirst (e e)

Inserts the specified element at the front of this deque. As the deque is unbounded, this method would never throw illegalstateexception.

public void AddLast (e e)

Inserts the specified element at the end of this deque. As the deque is unbounded, this method would never throw illegalstateexception.

public boolean Offerfirst (E e)

Inserts the specified element at the front of this deque. As the deque is unbounded, this method would never return false.

public boolean offerlast (E e)

Inserts the specified element at the end of this deque. As the deque is unbounded, this method would never return false.

Public E Peekfirst ()

Retrieves, but does isn't remove, the first element of this deque, or returns null if this deque is empty.

Public E Peeklast ()

Retrieves, but does isn't remove, the last element of this deque, or returns null if this deque is empty.

Public E GetFirst ()

Retrieves, but does isn't remove, the first element of this deque. This method, differs from Peekfirst , is throws a exception if this deque is empty.

Public E GetLast ()

Retrieves, but does isn't remove, the last element of this deque. This method, differs from Peeklast , is throws a exception if this deque is empty.

Public E Pollfirst ()

Retrieves and removes the first element of this deque, or returns null if this deque is empty.

Public E Polllast ()

Retrieves and removes the last element of this deque, or returns null if this deque is empty.

Public E Removefirst ()

Retrieves and removes the first element of this deque. This method, differs from Pollfirst , is throws a exception if this deque is empty.

Public E Removelast ()

Retrieves and removes the last element of this deque. This method, differs from Polllast , is throws a exception if this deque is empty.

Concurrentlinkedqueue

An unbounded Thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element, which has been on the queue the longest time. The tail of the queue is, that element, and the been on the queue the shortest time. New elements is inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the Queue. A Concurrentlinkedqueue is an appropriate choice when many threads would share access to a common collection. Like most other concurrent collection implementations, this class does not permit the use of null elements.

See more ...

Method Summary

Public boolean Add (E E)

Inserts the specified element at the tail of this queue. As the queue is unbounded, this method would never throw illegalstateexception or return false.

Public Boolean offer (E e)

Inserts the specified element at the tail of this queue. As the queue is unbounded, this method would never return false.

Public E Poll ()

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Public E-Peek ()

Retrieves, but does isn't remove, the head of this queue, or returns null if this queue is empty.

public boolean remove (Object o)

Removes a single instance of the specified element from this queue, if it is present. More formally, removes a element e such that o.equals (e), if this queue contains one or more such eleme Nts. Returns True if this queue contained the specified element (or equivalently, if this queue changed as a result of The call).

public int size ()

Returns the number of elements in this queue. If This queue contains more than integer.max_value elements, returns Integer.max_value.

Beware that, unlike in the most collections, the This method was not a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires an O (n) t Raversal. Additionally, if elements is added or removed during execution of this method, the returned result could be inaccurate< /c3>. Thus, this method was typically not very useful in concurrent applications.

Public Boolean contains (Object o)

Returns true if this queue contains the specified element. More formally, returns true if and if the this queue contains at least one element e such that o.equals (e) .

See more ...

Linkedblockingdeque

An optionally-bounded blocking deque based on linked nodes. The optional capacity bound constructor argument serves as a through to prevent excessive expansion. The capacity, if unspecified, is equal to integer.max_value. Linked nodes is dynamically created upon each insertion unless this would bring the deque above capacity.

See more ...

Linkedblockingqueue

An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element, which has been on the queue the longest time. The tail of the queue is, that element, and the been on the queue the shortest time. New elements is inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the Queue. Linked queues typically has higher throughput than array-based queues but less predictable performance in most concurrent Applications.

See more ...

Arrayblockingqueue

A bounded blocking queue backed by an array. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element, which has been on the queue the longest time. The tail of the queue is, that element, and the been on the queue the shortest time. New elements is inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the Queue.

This was a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by con Sumers. Once created, the capacity cannot be changed. Attempts to put an element into a full queue would result in the operation blocking; Attempts to take a element from a empty queue would similarly block.

This class supports a optional fairness policy for ordering waiting producer and consumer threads. By default, this ordering are not guaranteed. However, a queue constructed with fairness set to true grants threads access in FIFO order. Fairness generally decreases throughput but reduces variability and avoids starvation.

Priorityblockingqueue

an unbounded blocking queue that uses the same ordering rules as class outofmemoryerror ). This class does not permit null elements. A Priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so results in classcastexception ).

This class and its iterator implement all of the optional methods of the collection and iterator interfaces. the iterator provided in method iterator () is not guaranteed to traverse the elements of the arrays.sort (Pq.toarray ()) . Also, Method drainto can be used-remove some or all elements-order an D place them in another collection.

Operations on this class make no guarantees about the ordering of elements with equal priority. If you need to enforce an ordering, you can define custom classes or comparators this use a secondary key to break ties in Primary priority values.

See more ...

Java Concurrency-concurrent Collections

Related Article

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.