Java中的队列都有哪些,有什么区别。
2018-02-27 00:00
357 查看
Java中的队列都有哪些,实际上是问queue的实现有哪些,如:ConcurrentLinkedQueue、LinkedBlockingQueue 、ArrayBlockingQueue、LinkedList。
源于我经历过的一次生产事故,有个服务会收集业务系统的日志,此服务的开发人员在给业务系统的sdk中就因为使用了LinkedList,又没有做并发控制,就造成了此服务经常不能正常收集到业务系统的日志(丢日志以及日志上报的线程停止运行)。
看一下add()方法的源码,我们就可以知道原因了:
上面这个示例中,展示了LinkedList在多线程且没有做并发控制的环境下,size的值远远大于了队列的实际值,100个线程,每个添加1000个元素,最后实际只加进去2030个元素:
List的变量size值为:88371
第2031个元素取出为null
解决方案,使用锁或者使用ConcurrentLinkedQueue、LinkedBlockingQueue等支持添加元素为原子操作的队列。
我们已经分析过LinkedBlockingQueue的put等方法的源码,是使用ReentrantLock来实现的添加元素原子操作。那么再简单看一下高并发queue的add和offer()方法,方法中使用了CAS来实现的无锁的原子操作:
接下来,我们再利用高并发queue对上面的demo进行改造,大家只要改变demo中的内容,讲下面两行的注释内容颠倒,即可发现没有丢失任何的元素:
再看一下高性能queue的poll()方法,才觉得N
3ff0
B,取元素的方法也用CAS实现了原子操作,因此在实际使用的过程中,当我们不那么在意元素处理顺序的情况下,队列元素的消费者,完全可以是多个,不会丢任何数据:
关于取元素,ConcurrentLinkedQueue不支持阻塞去取元素,LinkedBlockingQueue支持阻塞的take()方法,如若大家需要ConcurrentLinkedQueue的消费者产生阻塞效果,需要自行实现
关于插入元素的性能,从字面上和代码简单的分析来看ConcurrentLinkedQueue肯定是最快的,但是这个也要看具体的测试场景,我做了两个简单的demo做测试,测试的结果如下,两个的性能差不多,但在实际的使用过程中,尤其在多cpu的服务器上,有锁和无锁的差距便体现出来了,ConcurrentLinkedQueue会比LinkedBlockingQueue快很多:ConcurrentLinkedQueuePerform:在使用ConcurrentLinkedQueue的情况下100个线程循环增加的元素数为:33828193
LinkedBlockingQueuePerform:在使用LinkedBlockingQueue的情况下100个线程循环增加的元素数为:33827382
源于我经历过的一次生产事故,有个服务会收集业务系统的日志,此服务的开发人员在给业务系统的sdk中就因为使用了LinkedList,又没有做并发控制,就造成了此服务经常不能正常收集到业务系统的日志(丢日志以及日志上报的线程停止运行)。
看一下add()方法的源码,我们就可以知道原因了:
public boolean add(E e) { linkLast(e);//调用linkLast,在队列尾部添加元素 return true; } void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++;//多线程情况下,如果业务系统没做并发控制,size的数量会远远大于实际元素的数量 modCount++; }
上面这个示例中,展示了LinkedList在多线程且没有做并发控制的环境下,size的值远远大于了队列的实际值,100个线程,每个添加1000个元素,最后实际只加进去2030个元素:
List的变量size值为:88371
第2031个元素取出为null
解决方案,使用锁或者使用ConcurrentLinkedQueue、LinkedBlockingQueue等支持添加元素为原子操作的队列。
我们已经分析过LinkedBlockingQueue的put等方法的源码,是使用ReentrantLock来实现的添加元素原子操作。那么再简单看一下高并发queue的add和offer()方法,方法中使用了CAS来实现的无锁的原子操作:
public boolean add(E e) { return offer(e); }
public boolean offer(E e) { checkNotNull(e); final Node<E> newNode = new Node<E>(e); for (Node<E> t = tail, p = t;;) { Node<E> q = p.next; if (q == null) { // p is last node if (p.casNext(null, newNode)) { // Successful CAS is the linearization point // for e to become an element of this queue, // and for newNode to become "live". if (p != t) // hop two nodes at a time casTail(t, newNode); // Failure is OK. return true; } // Lost CAS race to another thread; re-read next }else if (p == q) // We have fallen off list. If tail is unchanged, it // will also be off-list, in which case we need to // jump to head, from which all live nodes are always // reachable. Else the new tail is a better bet. p = (t != (t = tail)) ? t : head; else // Check for tail updates after two hops. p = (p != t && t != (t = tail)) ? t : q; } }
接下来,我们再利用高并发queue对上面的demo进行改造,大家只要改变demo中的内容,讲下面两行的注释内容颠倒,即可发现没有丢失任何的元素:
public static LinkedList list = new LinkedList(); //public static ConcurrentLinkedQueue list = new ConcurrentLinkedQueue();
再看一下高性能queue的poll()方法,才觉得N
3ff0
B,取元素的方法也用CAS实现了原子操作,因此在实际使用的过程中,当我们不那么在意元素处理顺序的情况下,队列元素的消费者,完全可以是多个,不会丢任何数据:
public E poll() { restartFromHead: for (;;) { for (Node<E> h = head, p = h, q;;) { E item = p.item; if (item != null && p.casItem(item, null)) { // Successful CAS is the linearization point // for item to be removed from this queue. if (p != h) // hop two nodes at a time updateHead(h, ((q = p.next) != null) ? q : p); return item; } else if ((q = p.next) == null) { updateHead(h, p); return null; } else if (p == q) continue restartFromHead; else p = q; } } }
关于ConcurrentLinkedQueue和LinkedBlockingQueue:
LinkedBlockingQueue是使用锁机制,ConcurrentLinkedQueue是使用CAS算法,虽然LinkedBlockingQueue的底层获取锁也是使用的CAS算法关于取元素,ConcurrentLinkedQueue不支持阻塞去取元素,LinkedBlockingQueue支持阻塞的take()方法,如若大家需要ConcurrentLinkedQueue的消费者产生阻塞效果,需要自行实现
关于插入元素的性能,从字面上和代码简单的分析来看ConcurrentLinkedQueue肯定是最快的,但是这个也要看具体的测试场景,我做了两个简单的demo做测试,测试的结果如下,两个的性能差不多,但在实际的使用过程中,尤其在多cpu的服务器上,有锁和无锁的差距便体现出来了,ConcurrentLinkedQueue会比LinkedBlockingQueue快很多:ConcurrentLinkedQueuePerform:在使用ConcurrentLinkedQueue的情况下100个线程循环增加的元素数为:33828193
LinkedBlockingQueuePerform:在使用LinkedBlockingQueue的情况下100个线程循环增加的元素数为:33827382
相关文章推荐
- 牛客网Java刷题知识点之什么是JSP的3大常用指令、JSP的6大哪些动作、JSP中include指令和include动作有什么区别
- java多线程中并发集合和同步集合有哪些?区别是什么?
- java 中 阻塞队列 非阻塞队列 和普通队列的区别是什么?
- JAVA所有修饰符有哪些?用在类,变量,方法前有什么区别?
- java的集合类有哪些他们之间的关系有什么区别
- java 中 阻塞队列 非阻塞队列 和普通队列的区别是什么?
- 牛客网Java刷题知识点之什么是HTTP协议、什么是HTTP隧道、HTTP响应的结构是怎么样的、HTTP报头包含哪些、HTTP中GET与POST方法有什么区别
- java的各个队列之间的联系和区别是什么
- java中int和Integer什么区别
- Java中JDK和JRE的区别是什么?
- 【Java面试题】39 Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?
- java中堆(heap)和堆栈(stack)有什么区别
- java运行库和JDK、java虚拟机有什么区别!
- Java中equal和==的区别是什么
- C++小知识:C++类中成员的访问控制属性有哪些类型,什么区别
- 【高薪就业必备06】 java中"=="和equals方法究竟有什么区别?
- java.exe和javaw.exe有什么区别?
- 【开发技术】 java和JSP和JavaScript有什么区别
- Javasrcipt,Ajax,Jquery用这些技术做了哪些东西,这些技术有什么优点,缺点
- java与javax有什么区别?