它是一个基于链接节点的无界线程安全队列。该队列的元素遵循先进先出的原则。头是最先加入的,尾是最近加入的。
插入元素是追加到尾上。提取一个元素是从头提取。当多个线程共享访问一个公共 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.
toArray
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
10还没有1个快,如果poll这后的业务逻辑运行时间小的话,多线程序没有任何意义,
反之如果poll这后的业务逻辑运行时间相当于Thread.sleep(1);多线程确实起作用!

ConcurrentLinkedQueue
是基于链接节点的、线程安全的队列。并发访问不需要同步。因为它在队列的尾部添加元素并从头部删除它们,所以只要不需要知道队列的大小,
ConcurrentLinkedQueue 对公共集合的共享访问就可以工作得很好。收集关于队列大小的信息会很慢,需要遍历队列。

/** 
     * 一个基于链接节点的、无界的、线程安全的队列。此队列按照 FIFO(先进先出)原则对元素进行排序。队列的头部 是队列中时间最长的元素。队列的尾部 
     * 是队列中时间最短的元素。新的元素插入到队列的尾部,队列检索操作从队列头部获得元素。当许多线程共享访问一个公共 collection 
     * 时,ConcurrentLinkedQueue 是一个恰当的选择。此队列不允许 null 元素。 
     */  
    private void concurrentLinkedQueueTest() {  
        ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();  
        concurrentLinkedQueue.add("a");  
        concurrentLinkedQueue.add("b");  
        concurrentLinkedQueue.add("c");  
        concurrentLinkedQueue.offer("d"); // 将指定元素插入到此队列的尾部。  
        concurrentLinkedQueue.peek(); // 检索并移除此队列的头,如果此队列为空,则返回 null。  
        concurrentLinkedQueue.poll(); // 检索并移除此队列的头,如果此队列为空,则返回 null。  
  
        for (String str : concurrentLinkedQueue) {  
            System.out.println(str);  
        }  
    }

最新文章

  1. [原创]Centos7 内部常用软件升级计划
  2. 获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址(服务器端)
  3. Eclipse 启动Tomcat 超时报错的解决方案
  4. Struts2 实战(一)
  5. [linux basic 基础]----同步信号量
  6. 7.Knockout.Js(Mapping插件)
  7. 【开源专访】Sea.js创始人玉伯的前端开发之路
  8. lua 中pairs 和 ipairs区别
  9. c随机数的产生(续)
  10. jstl--c:choose标签
  11. 递归实现生成Grey码
  12. [转载] 《Hadoop基础教程》之初识Hadoop
  13. Vue.js(一)了解Vue
  14. System.Web.Mvc 3.0.0.1 和 3.0.0.0 有什么区别?被 Microsoft ASP.NET MVC 的一次安全更新害惨了!!!
  15. 【Android手机测试】OOM
  16. nginx 关于client_max_body_size client_body_buffer_size配置
  17. java+appium 自动化环境搭建
  18. 基于vue的实战步骤
  19. scala 操作hdfs
  20. linux 创建守护进程的相关知识

热门文章

  1. Linux下SPI读写外部寄存器的操作
  2. GeoServer 常见问题总结 (转)
  3. 文字记录而已!!人民币直充/兑换PayPal美金
  4. 19. Spring Boot Shiro 权限管理
  5. 1.1selenium 介绍
  6. 【Codeforces Round #432 (Div. 2) A】 Arpa and a research in Mexican wave
  7. cocos2d-x 3.0 游戏关卡滑动 弹动 不会出现黑边效果
  8. noi25 最长最短单词(为什么会出现运行时错误)
  9. 关于Newtonsoft.json JsonConvert.DeserializeObject反序列化的使用
  10. HTTP网络协议(一)