目录
理解:可以看成是LinkedList的并发版本
底层原理:单项链表
构造方法
public ConcurrentLinkedQueue():首尾都是空的链表
public ConcurrentLinkedQueue(Collection c):把集合的元素放入链表
常用方法:
concurrentLinkedQueue.add("c"); 新增元素
boolean addAll(Collection c):添加集合
boolean contains(Object o):是否包含某个元素
isEmpty():是否为空
boolean remove(Object o):移除元素
int size():现有多少元素在链表中
Object[] toArray();转换成为数组
T[] toArray(T[] a):返回数组 a的元素在集合中存在的元素的数组
concurrentLinkedQueue.offer("d"); / 将指定元素插入到此队列的尾部。
concurrentLinkedQueue.peek(); 检索并不移除此队列的头,如果此队列为空,则返回 null。
concurrentLinkedQueue.poll(); //检索并移除此队列的头,如果此队列为空,则返回 null。
特点先进先出、无界,因为无界,所以不会阻塞(能存储数量没有限制)
demo
public class Test { public static void main(String[] args) { ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); queue.offer(6); System.out.println(queue); System.out.println("peek得到头元素不删除"+queue.peek()); System.out.println("peek得到头元素不删除"+queue); System.out.println("peek得到头元素并删除"+queue.poll()); System.out.println(queue); } }结果
[1, 2, 3, 4, 5, 6] peek得到头元素不删除1 peek得到头元素不删除[1, 2, 3, 4, 5, 6] peek得到头元素并删除1 [2, 3, 4, 5, 6]