Java中的ConcurrentLinkedQueue以及示例

Java中的ConcurrentLinkedQueue以及示例

Java中的 ConcurrentLinkedQueue 类是Java集合框架的一部分,它属于 java.util.concurrent 包。它在JDK 1.5中引入。它使用LinkedList并发地实现了Queue。它是一种 无限制线程安全 的Queue,以FIFO(先进先出)方式将元素插入到Queue的尾部。当无限制的Queue被多个线程共享时,可以使用它。这个类不允许空元素。迭代器是弱一致的。这个类及其迭代器实现了 QueueIterator 接口的所有可选方法。

类层次结构:

java.lang.Object
  ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractQueue<E>
        ↳ Class ConcurrentLinkedQueue<E>

Java中的ConcurrentLinkedQueue以及示例

声明:

public class ConcurrentLinkedQueue extends AbstractCollection implements Queue, Serializable

在这里, E 是此集合维护的元素的类型。

ConcurrentLinkedQueue的构造方法

要构造一个ConcurrentLinkedQueue,我们需要从 java.util.ConcurrentLinkedQueue 导入它。

1.ConcurrentLinkedQueue() :此构造函数用于构造一个空Queue。

ConcurrentLinkedQueue<E> clq = new ConcurrentLinkedQueue<E>();

**2.ConcurrentLinkedQueue(Collection c) **:此构造函数用于构造一个带有参数传递的集合的Queue。

ConcurrentLinkedQueue<E> clq = new ConcurrentLinkedQueue<E>(Collection<E> c);

以下是一个示例程序,用于说明Java中的ConcurrentLinkedQueue:

示例1:

// 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代码演示
// ConcurrentLinkedQueue的方法
 
import java.util.concurrent.*;
 
class ConcurrentLinkedQueueDemo {
   
    public static void main(String[] args)
    {
 
        // 使用ConcurrentLinkedQueue()构造函数创建ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer>
            clq = new ConcurrentLinkedQueue<Integer>();
 
        clq.add(12);
        clq.add(70);
        clq.add(1009);
        clq.add(475);
 
        // 显示现有的ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: "
                           + clq);
 
        // 使用peek()方法显示第一个元素
        System.out.println("第一个元素为: "
                           + clq.peek());
 
        // 使用poll()方法删除并显示第一个元素
        System.out.println("头部元素为: "
                           + clq.poll());
 
        // 显示现有的ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: "
                           + clq);
 
        // 使用size()方法获取大小
        System.out.println("大小: "
                           + clq.size());
    }
}

输出:

ConcurrentLinkedQueue: [12, 70, 1009, 475]
第一个元素为: 12
头部元素为: 12
ConcurrentLinkedQueue: [70, 1009, 475]
大小: 3

基本操作

1. 添加元素

ConcurrentLinkedQueue提供了两个方法来添加元素。

  • add():将传递的元素插入到此ConcurrentLinkedQueue的尾部。如果插入成功,则此方法返回true。ConcurrentLinkedQueue是无边界的,因此此方法永远不会引发IllegalStateException或返回false。
  • addAll():将传递的集合的所有元素插入到ConcurrentLinkedQueue的末尾。元素的插入顺序与集合迭代器返回的顺序相同。
// Java程序演示添加元素到
// ConcurrentLinkedQueue
 
import java.util.concurrent.*;
import java.util.*;
 
public class AddingElementsExample {
   
    public static void main(String[] args)
    {
 
        // 创建ConcurrentLinkedQueue的实例
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
 
        // 使用add()方法向队列添加字符串
        queue.add("Kolkata");
        queue.add("Patna");
        queue.add("Delhi");
        queue.add("Jammu");
 
        // 显示现有的ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
 
        // 创建一个字符串的ArrayList
        ArrayList<String> arraylist = new ArrayList<String>();
   
        // 向ArrayList添加字符串
        arraylist.add("Sanjeet");
        arraylist.add("Rabi");
        arraylist.add("Debasis");
        arraylist.add("Raunak");
        arraylist.add("Mahesh");
 
        // 显示现有的Collection
        System.out.println("要添加的集合为: " + arraylist);
 
        // 使用addAll()方法将ArrayList作为参数传递
        boolean response = queue.addAll(arraylist);
 
        // 显示现有的ConcurrentLinkedQueue
        System.out.println("添加的集合为: " + response);
 
        // 显示现有的ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
    }
}

输出:

ConcurrentLinkedQueue:[加尔各答,巴特那,德里,贾木]
要添加的集合:[Sanjeet,Rabi,Debasis,Raunak,Mahesh]
集合已添加:true
ConcurrentLinkedQueue:[加尔各答,巴特那,德里,贾木,Sanjeet,Rabi,Debasis,Raunak,Mahesh]

2. 删除元素

ConcurrentLinkedQueue 的 remove(Object o) 方法用于删除指定元素的单个实例(如果存在)。它移除了一个元素 e 使得 o.equals(e)。如果此 ConcurrentLinkedQueue 包含指定元素,则返回 true;否则返回 false。

// 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]
成功删除 Number 78249:true
更新后的 ConcurrentLinkedQueue:[4353,7824,8724]

3. 迭代元素

ConcurrentLinkedQueue 的 iterator() 方法用于以适当的顺序返回与此 ConcurrentLinkedQueue 中的相同元素的迭代器。从此方法返回的元素包含元素从第一个(head)到最后一个(tail)的顺序。返回的迭代器是弱一致的。

// 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]

迭代器的字符串值为:
Aman
Amar
Sanjeet
Rabi

4. 访问元素

peek() 和 element() 方法由 Queue 提供,用于访问 ConcurrentLinkedQueue 的元素。

element() 方法peek() 方法仅在队列为空时抛出异常上有所不同。

// Java程序演示访问ConcurrentLinkedQueue的元素

import java.util.*;
import java.util.concurrent.*;

public class AccessingElementsExample {

    public static void main(String[] args) throws IllegalStateException
    {
        // 创建ConcurrentLinkedQueue的实例
        ConcurrentLinkedQueue<Integer> Q = new ConcurrentLinkedQueue<>();

        // 将数字添加到队列末尾
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);

        // 打印队列
        System.out.println("队列:" + Q);

        // 打印头部
        System.out.println("队列的头部:" + Q.element());

        // 打印头部
        System.out.println("队列的头部:" + Q.peek());
    }
}

输出

队列:[7855642, 35658786, 5278367, 74381793]
队列的头部:7855642
队列的头部:7855642

ConcurrentLinkedQueue的方法

方法 描述
add​(E e) 将指定元素插入到队列的尾部。
addAll​(Collection c) | 将指定集合中的所有元素按照指定集合的迭代器返回它们的顺序附加到此队列的末尾。
contains​(Object o) | 如果此队列包含指定的元素,则返回true。
forEach​(Consumer action) | 对Iterable的每个元素执行给定的操作,直到所有元素都已处理或操作抛出异常。
isEmpty() | 如果此队列不包含元素,则返回true。
iterator() | 返回按照适当的顺序迭代此队列中的元素的迭代器。
offer​(E e) | 将指定元素插入到队列的尾部。
remove​(Object o) | 如果队列中存在指定的元素,则从此队列中删除它。
removeAll​(Collection c)
删除此集合中还包含在指定集合中的所有元素(可选操作)。
removeIf​(Predicate filter) | 移除集合中满足给定谓词的所有元素。
retainAll​(Collection c)
仅保留此集合中包含在指定集合中的元素(可选操作)。
size() 返回队列中的元素数量。
spliterator() 返回在此队列中的元素上滑动的Spliterator。
toArray() 以相应的顺序返回包含此队列中所有元素的数组。
toArray​(T[] a) 以正确的顺序返回包含此队列中所有元素的数组;返回的数组的运行时类型是指定数组的类型。

AbstractQueue类中声明的方法

方法 描述
clear() 从队列中移除所有元素。
element() 检索,但不删除,队列的头部元素。
remove() 检索并删除队列的头部元素。

java.util.AbstractCollection类中声明的方法

方法 描述
containsAll​(Collection c) | 如果此集合包含指定集合中的所有元素,则返回true。
toString() | 返回此集合的字符串表示形式。

### java.util.Collection接口中声明的方法

方法 | 描述
—|—
clear() | 删除此集合中的所有元素(可选操作)。
containsAll​(Collection c)

如果此集合包含指定集合中的所有元素,则返回true。
equals​(Object o) 将指定的对象与此集合比较以实现相等性。
hashCode() 返回此集合的哈希码值。
parallelStream() 返回具有此集合作为其源的可能并行流。
stream() 返回具有此集合作为其源的串行流。
toArray​(IntFunction<T[]> generator) 使用提供的生成器函数分配返回的数组,返回包含此集合中所有元素的数组。

java.util.Queue接口中声明的方法

方法 描述
element() 检索,但不删除,队列的头部元素。
peek() 检索,但不删除,队列的头部元素;如果此队列为空,则返回null。
poll() 检索并删除队列的头部元素;如果此队列为空,则返回null。
remove() 检索并删除队列的头部元素。

参考: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/ConcurrentLinkedQueue.html

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程