ConcurrentLinkedQueueJava 中的類是Java集合框架。它屬於java.util.concurrent包。它是在 JDK 1.5 中引入的。它用於實現隊列借助LinkedList同時。它是一個無界的 線程安全Queue 的實現,以 FIFO(先進先出)方式在隊列尾部插入元素。當無界隊列在多個線程之間共享時可以使用它。該類不允許空元素。迭代器是弱一致的。這個類及其迭代器實現了所有可選方法隊列和迭代器接口。
類層次結構:
java.lang.Object
  ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractQueue<E>
        ↳ Class ConcurrentLinkedQueue<E>

聲明:
public class ConcurrentLinkedQueue<E> extends AbstractCollection<E> implements Queue<E>, Serializable
這裏,E是該集合維護的元素的類型。
ConcurrentLinkedQueue 的構造函數
要構造一個 ConcurrentLinkedQueue,我們需要將其導入java.util.ConcurrentLinkedQueue.
1.ConcurrentLinkedQueue():該構造函數用於構造一個空隊列。
ConcurrentLinkedQueue<E> clq = new ConcurrentLinkedQueue<E>();
2. ConcurrentLinkedQueue(Collection<E> c):該構造函數用於構造一個隊列,並將 Collection 的元素作為參數傳遞。
ConcurrentLinkedQueue<E> clq = new ConcurrentLinkedQueue<E>(Collection<E> c);
下麵是一個示例程序,用於說明 Java 中的ConcurrentLinkedQueue:
示例 1:
Java
// Java program to demonstrate ConcurrentLinkedQueue
import java.util.concurrent.*;
class ConcurrentLinkedQueueDemo {
   
    public static void main(String[] args)
    {
        // Create a ConcurrentLinkedQueue
        // using ConcurrentLinkedQueue() constructor
        ConcurrentLinkedQueue<Integer>
            clq = new ConcurrentLinkedQueue<Integer>();
        clq.add(12);
        clq.add(70);
        clq.add(1009);
        clq.add(475);
        // Displaying the existing  ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: "
                           + clq);
        // Create a ConcurrentLinkedQueue
        // using ConcurrentLinkedQueue(Collection c) 
        // constructor
        ConcurrentLinkedQueue<Integer>
            clq1 = new ConcurrentLinkedQueue<Integer>(clq);
        // Displaying the existing  ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue1: "
                           + clq1);
    }
}ConcurrentLinkedQueue: [12, 70, 1009, 475] ConcurrentLinkedQueue1: [12, 70, 1009, 475]
示例 2:
Java
// Java code to illustrate
// methods of ConcurrentLinkedQueue
import java.util.concurrent.*;
class ConcurrentLinkedQueueDemo {
   
    public static void main(String[] args)
    {
        // Create a ConcurrentLinkedQueue
        // using ConcurrentLinkedQueue() 
          // constructor
        ConcurrentLinkedQueue<Integer>
            clq = new ConcurrentLinkedQueue<Integer>();
        clq.add(12);
        clq.add(70);
        clq.add(1009);
        clq.add(475);
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: "
                           + clq);
        // Displaying the first element
        // using peek() method
        System.out.println("First Element is: "
                           + clq.peek());
        // Remove and display the first element
        // using poll() method
        System.out.println("Head Element is: "
                           + clq.poll());
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: "
                           + clq);
        // Get the size using size() method
        System.out.println("Size: "
                           + clq.size());
    }
}ConcurrentLinkedQueue: [12, 70, 1009, 475] First Element is: 12 Head Element is: 12 ConcurrentLinkedQueue: [70, 1009, 475] Size: 3
Basic Operations
1. 添加元素
添加元素ConcurrentLinkedQueue提供了兩種方法。
- add() 它將插入元素,並作為參數傳遞到此 ConcurrentLinkedQueue 的尾部。如果插入成功,該方法返回 True。 ConcurrentLinkedQueue 是無界的,因此此方法永遠不會拋出 IllegalStateException 或返回 false。
 - addAll() 它插入 Collection 的所有元素,並作為參數傳遞到 ConcurrentLinkedQueue 的末尾。元素的插入順序與集合迭代器返回的順序相同。
 
Java
// Java Program Demonstrate adding
// elements to ConcurrentLinkedQueue
import java.util.concurrent.*;
import java.util.*;
public class AddingElementsExample {
   
    public static void main(String[] args)
    {
        // Create an instance of ConcurrentLinkedQueue
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
        // Add String to queue using add method
        queue.add("Kolkata");
        queue.add("Patna");
        queue.add("Delhi");
        queue.add("Jammu");
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
        // create a ArrayList of Strings
        ArrayList<String> arraylist = new ArrayList<String>(); 
   
        // add String to ArrayList 
        arraylist.add("Sanjeet"); 
        arraylist.add("Rabi"); 
        arraylist.add("Debasis"); 
        arraylist.add("Raunak"); 
        arraylist.add("Mahesh"); 
        // Displaying the existing Collection
        System.out.println("Collection to be added: " + arraylist);
        // apply addAll() method and passed
        // the arraylist as parameter
        boolean response = queue.addAll(arraylist);
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("Collection added: " + response);
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
    }
}ConcurrentLinkedQueue: [Kolkata, Patna, Delhi, Jammu] Collection to be added: [Sanjeet, Rabi, Debasis, Raunak, Mahesh] Collection added: true ConcurrentLinkedQueue: [Kolkata, Patna, Delhi, Jammu, Sanjeet, Rabi, Debasis, Raunak, Mahesh]
2. 刪除元素
ConcurrentLinkedQueue remove()ConcurrentLinkedQueue 的方法用於刪除指定元素的單個實例(如果存在)。它刪除一個元素e使得o.等於(e)。如果 ConcurrentLinkedQueue 包含指定元素,則返回 true,否則返回 false。
Java
// Java Program Demonstrate removing
// elements from ConcurrentLinkedQueue
import java.util.concurrent.*;
public class RemovingElementsExample {
   
    public static void main(String[] args)
    {
        // Create an instance of ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
        // Add Numbers to queue using add(e) method
        queue.add(4353);
        queue.add(7824);
        queue.add(78249);
        queue.add(8724);
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
        // apply remove() for Number 78249
        boolean response = queue.remove(78249);
        // print results
        System.out.println("Removing Number 78249 successful: " + response);
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("Updated ConcurrentLinkedQueue: " + queue);
    }
}ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] Removing Number 78249 successful: true Updated ConcurrentLinkedQueue: [4353, 7824, 8724]
3. 迭代元素
ConcurrentLinkedQueue 的 iterator() 方法用於以適當的順序返回與 ConcurrentLinkedQueue 相同元素的迭代器。從此方法返回的元素包含按從第一個(頭)到最後一個(尾)的順序排列的元素。返回的迭代器是弱一致的。
Java
// Java Program Demonstrate Iterating
// over ConcurrentLinkedQueue
import java.util.concurrent.*;
import java.util.*;
public class TraversingExample {
   
    public static void main(String[] args)
    {
        // Create an instance of ConcurrentLinkedQueue
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
        // Add String to queue using add(e) method
        queue.add("Aman");
        queue.add("Amar");
        queue.add("Sanjeet");
        queue.add("Rabi");
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue : " + queue);
        // Call iterator() method
        Iterator iterator = queue.iterator();
        // Print elements of iterator
        System.out.println("\nThe String Values of iterator are:");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}ConcurrentLinkedQueue : [Aman, Amar, Sanjeet, Rabi] The String Values of iterator are: Aman Amar Sanjeet Rabi
4. 訪問元素
Queue peek()和Queue element()提供的方法隊列用於訪問 ConcurrentLinkedQueue 的元素。
element() 方法不同於peek()方法隻是如果該隊列為空,它會拋出異常。
Java
// Java Program Demonstrate accessing
// elements of ConcurrentLinkedQueue
import java.util.*;
import java.util.concurrent.*;
public class AccessingElementsExample {
   
    public static void main(String[] args) throws IllegalStateException
    {
        // Create an instance of ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer> Q = new ConcurrentLinkedQueue<>();
        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);
        // print queue
        System.out.println("Queue: " + Q);
        // print head
        System.out.println("Queue's head: " + Q.element());
        // print head
        System.out.println("Queue's head: " + Q.peek());
    }
}Queue: [7855642, 35658786, 5278367, 74381793] Queue's head: 7855642 Queue's head: 7855642
ConcurrentLinkedQueue的方法
| 
 METHOD  | 
 DESCRIPTION  | 
|---|---|
| add(E e) | 將指定元素插入此隊列的尾部。 | 
| addAll(集合 <? 擴展 E> c) | 將指定集合中的所有元素按照指定集合的迭代器返回的順序追加到此隊列的末尾。 | 
| contains(Object o) | 如果此隊列包含指定元素,則返回 true。 | 
| forEach(消費者<? super E> 操作) | 對 Iterable 的每個元素執行給定的操作,直到處理完所有元素或該操作引發異常。 | 
| isEmpty() | 如果此隊列不包含元素,則返回 true。 | 
| iterator() | 按正確的順序返回此隊列中元素的迭代器。 | 
| offer(E e) | 將指定元素插入此隊列的尾部。 | 
| remove(Object o) | 從此隊列中刪除指定元素的單個實例(如果存在)。 | 
| 移除全部(集合<?> c) | 刪除指定集合中也包含的所有該集合的元素(可選操作)。 | 
| removeIf(Predicate<? super E> 過濾器) | 刪除此集合中滿足給定謂詞的所有元素。 | 
| keepAll(集合<?> c) | 僅保留此集合中包含在指定集合中的元素(可選操作)。 | 
| size() | 返回此隊列中的元素數量。 | 
| spliterator() | 返回此隊列中元素的Spliterator。 | 
| ConcurrentLinkedQueue toArray() | 返回一個數組,其中包含此隊列中的所有元素(按正確的順序)。 | 
| ConcurrentLinkedQueue toArray() | 返回一個數組,其中包含此隊列中的所有元素(按正確的順序);返回數組的運行時類型是指定數組的運行時類型。 | 
類 java.util.AbstractQueue 中聲明的方法
| 
 METHOD  | 
 DESCRIPTION  | 
|---|---|
| AbstractQueue clear() | 從此隊列中刪除所有元素。 | 
| AbstractQueue element() | 檢索但不刪除此隊列的頭部。 | 
| AbstractQueue remove() | 檢索並刪除此隊列的頭部。 | 
類 java.util.AbstractCollection 中聲明的方法
| 
 METHOD  | 
 DESCRIPTION  | 
|---|---|
| AbstractCollection containsAll() | 如果此集合包含指定集合中的所有元素,則返回 true。 | 
| AbstractCollection toString() | 返回此集合的字符串表示形式。 | 
接口 java.util.Collection 中聲明的方法
| 
 METHOD  | 
 DESCRIPTION  | 
|---|---|
| Collection clear() | 從此集合中刪除所有元素(可選操作)。 | 
| containsAll(集合<?> c) | 如果此集合包含指定集合中的所有元素,則返回 true。 | 
| equals(Object o) | 比較指定對象與此集合是否相等。 | 
| hashCode() | 返回此集合的哈希碼值。 | 
| parallelStream() | 返回一個可能並行的 Stream 並以此集合作為其源。 | 
| stream() | 返回以此集合作為源的順序 Stream。 | 
| toArray(IntFunction<T[]> 生成器) | 返回一個包含此集合中所有元素的數組,使用提供的生成器函數分配返回的數組。 | 
接口 java.util.Queue 中聲明的方法
| 
 METHOD  | 
 DESCRIPTION  | 
|---|---|
| Queue element() | 檢索但不刪除此隊列的頭部。 | 
| Queue peek() | 檢索但不刪除此隊列的頭部,如果此隊列為空,則返回 null。 | 
| Queue poll() | 檢索並刪除此隊列的頭部,如果此隊列為空,則返回 null。 | 
| Queue remove() | 檢索並刪除此隊列的頭部。 | 
相關用法
- Java ConcurrentLinkedQueue forEach()用法及代碼示例
 - Java ConcurrentLinkedQueue removeAll()用法及代碼示例
 - Java ConcurrentLinkedQueue removeIf()用法及代碼示例
 - Java ConcurrentLinkedQueue retainAll()用法及代碼示例
 - Java ConcurrentLinkedQueue add()用法及代碼示例
 - Java ConcurrentLinkedQueue addAll()用法及代碼示例
 - Java ConcurrentLinkedQueue contains()用法及代碼示例
 - Java ConcurrentLinkedQueue isEmpty()用法及代碼示例
 - Java ConcurrentLinkedQueue iterator()用法及代碼示例
 - Java ConcurrentLinkedQueue offer()用法及代碼示例
 - Java ConcurrentLinkedQueue peek()用法及代碼示例
 - Java ConcurrentLinkedQueue poll()用法及代碼示例
 - Java ConcurrentLinkedQueue remove()用法及代碼示例
 - Java ConcurrentLinkedQueue size()用法及代碼示例
 - Java ConcurrentLinkedQueue spliterator()用法及代碼示例
 - Java ConcurrentLinkedQueue toArray()用法及代碼示例
 - Java ConcurrentLinkedDeque add()用法及代碼示例
 - Java ConcurrentLinkedDeque addAll()用法及代碼示例
 - Java ConcurrentLinkedDeque addFirst()用法及代碼示例
 - Java ConcurrentLinkedDeque addLast()用法及代碼示例
 - Java ConcurrentLinkedDeque clear()用法及代碼示例
 - Java ConcurrentLinkedDeque contains()用法及代碼示例
 - Java ConcurrentLinkedDeque descendingIterator()用法及代碼示例
 - Java ConcurrentLinkedDeque element()用法及代碼示例
 - Java ConcurrentLinkedDeque equals()用法及代碼示例
 
注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 ConcurrentLinkedQueue in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
