ublic class
ConcurrentLinkedQueue
extends AbstractQueue<E>
implements Serializable Queue<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.ConcurrentLinkedQueue<E>
Class Overview
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.
This queue does not permit null elements.
它是一个基于链接节点的无界线程安全队列。该队列的元素遵循先进先出的原则。头是最先加入的,尾是最近加入的。
插入元素是追加到尾上。提取一个元素是从头提取。当多个线程共享访问一个公共 collection 时,ConcurrentLinkedQueue 是一个恰当的选择。该队列不允许null元素。
This implementation employs an efficient "wait-free" algorithm based on one described in Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms by .
此实现采用了有效的“无等待 (wait-free)”算法,该算法基于 Maged M. Michael 和 Michael L. Scott 合著的 Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms 中描述的算法。
Beware that, unlike in most collections, the size method is NOT a constant-time operation. Because of the asynchronous nature of these queues,
determining the current number of elements requires a traversal of the elements.
注意和大多数集合不一样。得到元素个数所花的时间是不确定。由于该队列的异步特性,确定当前的元素数量需要遍历的元素。
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.
这个类和它的迭代器实现了Collection和Iterator接口的所有可选方法。
Memory consistency effects: As with other concurrent collections,
actions in a thread prior to placing an object into a ConcurrentLinkedQueue happen-before actions subsequent to the access or removal of that element from the ConcurrentLinkedQueue in another thread.
内存一致性效果:和其他并发集合一样。把一个元素放入到队列的线程的优先级高与对元素的访问和移除的线程。
有三个函数需要注意的:
E peek()
Gets but does not remove the element at the head of the queue.
E poll()
Gets and removes the element at the head of the queue, or returns null if there is no element in the queue.
public <T> T[] toArray(T[] a)
返回以恰当顺序包含此队列所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。如果指定的数组能容纳队列,则将该队列返回此处。否则,将分配一个具有指定数组的运行时类型和此队列大小的新数组。
如果指定的数组能容纳队列,并有剩余的空间(即数组的元素比队列多),那么会将紧接队列尾部的元素设置为 null。
像 toArray() 方法一样,此方法充当基于数组的 API 与基于 collection 的 API 之间的桥梁。更进一步说,此方法允许对输出数组的运行时类型进行精确控制,在某些情况下,这可以用来节省分配开销。
假定 x 是只包含字符串的一个已知队列。以下代码用来将该队列转储到一个新分配的 String 数组:
String[] y = x.toArray(new String[0]);
注意,toArray(new Object[0]) 和 toArray() 在功能上是相同的。
指定者:
接口 Collection<E> 中的 toArray
覆盖:
类 AbstractCollection<E> 中的 toArray
参数:
a - 将用来存储队列元素的数组(如果该数组足够大);否则,将为此分配一个具有相同运行时类型的新数组
返回:
包含此队列所有元素的数组
抛出:
ArrayStoreException - 如果指定数组的运行时类型不是此队列中每个元素的运行时类型的超类型
NullPointerException - 如果指定数组为 null
size
public int size()
返回此队列中的元素数量。如果此队列包含的元素数大于 Integer.MAX_VALUE,则返回 Integer.MAX_VALUE。
需要小心的是,与大多数 collection 不同,此方法不是 一个固定时间操作。由于这些队列的异步特性,确定当前的元素数需要进行一次花费 O(n) 时间的遍历。
指定者:
接口 Collection<E> 中的 size
指定者:
类 AbstractCollection<E> 中的 size
返回:
此队列中的元素数
注意1:
ConcurrentLinkedQueue的.size() 是要遍历一遍集合的,很慢的,所以尽量要避免用size,
如果判断队列是否为空最好用isEmpty()而不是用size来判断.
注意问题1:
使用了这个ConcurrentLinkedQueue 类之后是否意味着我们不需要自己进行任何同步或加锁操作了呢?
我在网上找到了这个:http://stackoverflow.com/questions/435069/java-util-concurrentlinkedqueue/435941 // StackOverflow果然是个好地方啊……
也就是说,如果直接使用它提供的函数,比如:queue.add(obj); 或者 queue.poll(obj);,这样我们自己不需要做任何同步。
但如果是非原子操作,比如:
1. if(!queue.isEmpty()) {
2. queue.poll(obj);
3. }
我们很难保证,在调用了isEmpty()之后,poll()之前,这个queue没有被其他线程修改。
所以对于这种情况,我们还是需要自己同步:
1. synchronized(queue) {
2. if(!queue.isEmpty()) {
3. queue.poll(obj);
4. }
5. }
注意问题2:
主类
Java代码
1. public class conn{
2. public static void main(String[] args) throws Exception{
3. Queue<String> queue=new ConcurrentLinkedQueue<String>();
4. for(int i=0;i<1000000;i++){
5. queue.add(String.valueOf(i));
6. }
7. int num=10;//线程人个数
8. for(int i=0;i<num;i++){
9. new ThreadConn(queue);
10. }
11.
12. }
13. }
线程类
Java代码
1. public class ThreadConn implements Runnable{
2. Queue<String> queue;
3. public ThreadConn(Queue<String> queue){
4. this.queue=queue;
5. Thread thread=new Thread(this);
6. thread.start();
7. }
8. public void run(){
9. try{
10. long sd=new Date().getTime();
11. while(queue.poll()!=null){
12. //这里是业务逻辑
13. }
14. System.out.println (sn-sd);
15. }catch(Exception e){
16. e.printStackTrace();
17. }
18. }
19. }
测试结果如下:
启动10个线程
31
0
0
0
0
500
390
297
0
0
启动1个线程
360
分享到:
相关推荐
ConcurrentLinkedQueue源码分析
ConcurrentLinkedQueue
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
自己动手让springboot异步处理浏览器发送的请求(只需要使用ConcurrentLinkedQueue即可)
LinkedBlockingQueue 首先 LinkedBlockingQueue 是一个 “可选且有界” 的阻塞队列实现,你可以根据需要指定队列的大小。 接下来,我将创建一个 LinkedBlockingQueue ,它最多可以包含100个元素: ...
ConcurrentLinkedQueue 在考虑并发的时候可以先考虑单线程的情况,然后再将并发的情况考虑进来。 比如ConcurrentLinkedQueue: 1、先考虑单线的offer 2、再考虑多线程时候的offer: · 多个线程offer...
聊聊并发(6)ConcurrentLinkedQueue的实现原理分析Java开发Java经验技巧共18页.pdf.zip
主要介绍了Java concurrency集合之ConcurrentLinkedQueue,需要的朋友可以参考下
Java 多线程与并发(15_26)-JUC集合_ ConcurrentLinkedQueue详解
随着项目的不断迭代,加上产品经理大法(这里加一个弹窗提示,...为了防止多个线程同时操作DialogManager中的queue对象,所以我们采用线程安全的ConcurrentLinkedQueue,这里简单的介绍下ConcurrentLinkedQueue实现和数
java.util.concurrent总体概览图。 收取资源分3分。需要的同学可以下载一下。 java.util.concurrent主要包括5个部分executor,colletions,locks,atomic,tools。 该图详细的列举了并发包下面的结构,包含所有接口和...
ConcurrentLinkedQueue是⼀个基于链接节点的⽆界线程安全队列,它采⽤先进先出的规则对节点 进⾏排序,当我们添加⼀个元素的时候,它会添加到队列的尾部,当我们获取⼀个元素时,它会返 回队列头部的元素。 ● ...
15.并发容器之ConcurrentLinkedQueue 16.并发容器之CopyOnWriteArrayList 17.并发容器之ThreadLocal 18.一篇文章,从源码深入详解ThreadLocal内存泄漏问题 19.并发容器之BlockingQueue 20.并发容器之...
可用和获取的资源在单独的数据结构中进行跟踪 - ConcurrentLinkedQueue。 ConcurrentLinkedQueue 提供无界数据结构,因此托管资源可以根据需要增长。 ConcurrentLinkedQueue 提供高效的多线程访问。 队列数据结构...
Java并发编程常见知识点源码集锦,涉及到对象锁,Executors多任务线程框架,线程池等... ConcurrentLinkedQueue、DelayQueue示例、自定义的线程拒绝策略、自定义线程池(使用有界队列)、自定义线程池(使用无界队列)。。。
一个简单的jdbc连接池,考虑到线程安全,这里使用了ConcurrentLinkedQueue,新手入门,简单易懂
6. 聊聊并发(六)ConcurrentLinkedQueue的实现原理 7. 聊聊并发(七)Java中的阻塞队列 8. 聊聊并发(八)Fork/Join框架介绍 9. 聊聊并发(九)Java中的CopyOnWrite容器 10. 聊聊并发(十)生产者消费者模式
JDK源码阅读笔记 Condition CountDownLatch CyclicBarrier ...ConcurrentLinkedQueue ThreadPool ThreadLocal Reflection ScheduledThreadPool AsynchronousFileChannel BufferedInputStream Enum
- **`ConcurrentLinkedQueue`** : 高效的并发队列,使用链表实现。可以看做一个线程安全的 `LinkedList`,这是一个非阻塞队列。 - **`BlockingQueue`** : 这是一个接口,JDK 内部通过链表、数组等方式实现了这个接口...
重新打包编译,解决了并发生抛错问题 java.util.LinkedList$ListItr.checkForComodification Queue<String> responeBacklog = new ConcurrentLinkedQueue();