Data structures are 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 you can use in your programs.

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

Java provides data collections that you can use in your concurrent programs without any problems or inconsistency. Basically, Java provides two 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 is full or empty, the thread that makes the call will 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 will 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 will 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 will 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 will 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 will never return false.

public E peekFirst()

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

public E peekLast()

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

public E getFirst()

Retrieves, but does not remove, the first element of this deque. This method differs from peekFirst only in that it throws an exception if this deque is empty.

public E getLast()

Retrieves, but does not remove, the last element of this deque. This method differs from peekLast only in that it throws an 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 only in that it throws an exception if this deque is empty.

public E removeLast()

Retrieves and removes the last element of this deque. This method differs from pollLast only in that it throws an 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 that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are 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 will 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 will 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 will 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 not 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 an element e such that o.equals(e), if this queue contains one or more such elements. 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 most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires an O(n) traversal. Additionally, if elements are added or removed during execution of this method, the returned result may be inaccurate. Thus, this method is 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 only if 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 way to prevent excessive expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are 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 that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have 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 that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue.

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

This class supports an optional fairness policy for ordering waiting producer and consumer threads. By default, this ordering is 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 PriorityQueue and supplies blocking retrieval operations. While this queue is logically unbounded, attempted additions may fail due to resource exhaustion (causing 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 PriorityBlockingQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()). Also, method drainTo can be used to remove some or all elements in priority order and 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 that use a secondary key to break ties in primary priority values.

See More...

Java Concurrency - Concurrent Collections的更多相关文章

  1. Java Concurrency - java.util.concurrent API Class Diagram

    摘自: www.uml-diagrams.org Here we provide several UML class diagrams for the Java™ 7 java.util.concur ...

  2. Java Concurrency - Phaser, Controlling phase change in concurrent phased tasks

    The Phaser class provides a method that is executed each time the phaser changes the phase. It's the ...

  3. [Java concurrent][Collections]

    同步容器类 同步容器类包括Vector和Hashtable,二者是早期JDK的一部分.以及一些在JDK1.2中添加的可以由Collections.synchronizedXxx等工厂方法创建的. 这些 ...

  4. java 多线程 集合的包装方法Collections.synchronizedXXXXX;线程安全的集合类:Java.util.concurrent.ConcurrentXXX;java.util.concurrent.CopyOnWriteXXXX

    问题:ArrayList  等线程不安全 当多线程并发修改一个集合数据时,可能同一个下标位置被覆盖. 示例代码: 一个List,我们创建10个线程,每个线程往这个List中添加1000条数据,结果往往 ...

  5. jdk8中java.util.concurrent包分析

    并发框架分类 1. Executor相关类 Interfaces. Executor is a simple standardized interface for defining custom th ...

  6. 《深入浅出 Java Concurrency》—并发容器 ConcurrentMap

    (转自:http://blog.csdn.net/fg2006/article/details/6404226) 在JDK 1.4以下只有Vector和Hashtable是线程安全的集合(也称并发容器 ...

  7. 深入浅出 Java Concurrency (35): 线程池 part 8 线程池的实现及原理 (3)[转]

    线程池任务执行结果 这一节来探讨下线程池中任务执行的结果以及如何阻塞线程.取消任务等等. 1 package info.imxylz.study.concurrency.future;2 3 publ ...

  8. 深入浅出 Java Concurrency (16): 并发容器 part 1 ConcurrentMap (1)[转]

    从这一节开始正式进入并发容器的部分,来看看JDK 6带来了哪些并发容器. 在JDK 1.4以下只有Vector和Hashtable是线程安全的集合(也称并发容器,Collections.synchro ...

  9. java.util.concurrent

    软件包 java.util.concurrent 的描述 在并发编程中很常用的实用工具类.此包包括了几个小的.已标准化的可扩展框架,以及一些提供有用功能的类,没有这些类,这些功能会很难实现或实现起来冗 ...

随机推荐

  1. Linux vmstat:报告虚拟内存统计的工具

    众所周知,计算机必须有称之为RAM(随机访问内存)的存储器使得计算机工作.RAM指的是插在计算机主板上的物理存储.这里的RAM被用于加载像浏览器.文字处理器这类的程序,实际上,你使用的程序都运行在内存 ...

  2. UVa 1640 The Counting Problem (数学,区间计数)

    题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次. 析:看起来挺简单的,其实并不好做,因为有容易想乱了.主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来.都从 ...

  3. Debug Tools

    .NET专用调试工具:MDBG .NET的死锁调试工具:ACorns.Debugging WinDBG+SOS(Windows平台下最强DeBug工具,是解决BUG的最后手段)

  4. RIA(富客户端)发展态势

    在过去的两到三年中,Web开发人员一直是想构建一种比传统HTML更丰富的客户端:这是一个用户接口,它比用HTML能实现的接口更加健壮.反应更加灵敏和更具有令人感兴趣的可视化特性.RIA技术的出现允许我 ...

  5. 如何在Android中使用OpenCV

    如何在Android中使用OpenCV 2011-09-21 10:22:35 标签:Android 移动开发 JNI OpenCV NDK 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...

  6. 命令行创建maven模块工程

    上一边文章,借助外部eclipse来创建模块项目,本文直接使用maven命令来创建 mvn archetype:generate -DgroupId=com.mycompany.demo -Darti ...

  7. js中的if判断十分优美的简洁写法

    本尊混迹猿人类也有5年有余,从最开始的C#到java再到php到至今的python,不能说精通,也算得上是熟悉,对各个语言的语法也算是了解. 虽然目前在开发web程序,了解一些java知识,但是今天在 ...

  8. rxjava各种使用场景

    1. 数据的三级缓存 final Observable memory = Observable.create(new Observable.OnSubscribe() { @Override publ ...

  9. 应用XML作为数据库的快速开发框架

    背景 我经常应用C#开发一些小的桌面程序,这些桌面程序往往有以下几个特点: 程序比较小,开发周期很短. 程序的数据量不大,多数情况下不超过1万行记录. 对程序的性能要求不高. 程序并发很少或者基本没有 ...

  10. Oracle DataGuard 物理Standby 搭建(下)

    主备库切换 Switchover 一般SWITCHOVER切换都是计划中的切换,特点是在切换后,不会丢失任何的数据,而且这个过程是可逆的,整个DATA GUARD环境不会被破坏,原来DATA GUAR ...