目錄
public ConcurrentLinkedQueue():首尾都是空的鏈表
public ConcurrentLinkedQueue(Collection c):把集合的元素放入鏈表
concurrentLinkedQueue.add("c"); 新增元素
boolean addAll(Collection c):添加集合
boolean contains(Object o):是否包含某個元素
T[] toArray(T[] a):返回數組 a的元素在集合中存在的元素的數組
concurrentLinkedQueue.offer("d"); / 將指定元素插入到此隊列的尾部。
concurrentLinkedQueue.peek(); 檢索並不移除此隊列的頭,如果此隊列爲空,則返回 null。
concurrentLinkedQueue.poll(); //檢索並移除此隊列的頭,如果此隊列爲空,則返回 null。
理解:可以看成是LinkedList的併發版本
底層原理:單項鍊表
特點先進先出、無界,因爲無界,所以不會阻塞(能存儲數量沒有限制)
構造方法
public ConcurrentLinkedQueue():首尾都是空的鏈表
public ConcurrentLinkedQueue(Collection<? extends E> c):把集合的元素放入鏈表
常用方法:
concurrentLinkedQueue.add("c"); 新增元素
boolean addAll(Collection<? extends E> c):添加集合
boolean contains(Object o):是否包含某個元素
isEmpty():是否爲空
boolean remove(Object o):移除元素
int size():現有多少元素在鏈表中
Object[] toArray();轉換成爲數組
<T> 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]