实现 ConcurrentLinkedDeque API 的Java程序
Java中的 ConcurrentLinkedDeque 类是一个无界并发双端队列,它将其元素存储为链接节点,其中每个节点都包含前一个和下一个节点的地址。它属于Java .util.concurrent包。此类是Java集合框架的成员。它还扩展了 Object 和 AbstractCollection 类。
ConcurrentLinkedDeque API 的特点
- 它不允许空元素。
- 迭代器是弱一致的。
- 并发插入、删除和访问操作在多个线程间安全执行,因此它是线程安全的。
- size 方法不是恒定时间操作
实现接口
1. Serializable
2. Iterable
3. Collection
4. Deque
5. Queue
参数: E — 集合中元素的类型
句法:
public class ConcurrentLinkedDeque
extends AbstractCollection
implements Deque, Serializable
构造函数:
- public ConcurrentLinkedDeque():它创建一个空的双端队列。
- public ConcurrentLinkedDeque(Collection
c):它创建一个最初包含 Collection 元素的双端队列。
方法:Method Type Description add(E e) boolean Inserts an element in the tail of the deque addAll(Collection boolean Inserts all the elements present in the specified Collection addFirst(E e) void Adds an element in the front of the deque addLast(E e) void Adds an element in the last of the deque clear() void Removes all the elements from the deque contains(Object o) boolean Returns true if the deque contains the Object O descendingIterator() Iterator Returns an iterator over the elements in the deque in reverse order. element() E Retrieves the head of the deque without removing it getFirst() E Retrieves the first element of the deque getLast() E Retrieves the last element of the deque isEmpty() boolean Returns true if the deque contains no elements iterator() Iterator Returns an iterator over the elements in the deque peek() E Retrieves the head of the deque without removing it poll() E Retrieves and removes the head of the deque push(E e) void Pushes an element onto the stack represented by the deque pop() E Pops an element from the stack represented by the deque. remove() E Retrieves and removes the head of the queue size() int Returns the size of the deque toArray() Object[] Returns an array containing all of the elements in the deque
执行:
例子
Java
// Java Program to Implement ConcurrentLinkedDeque API
// Importing all classes from
// java.util package
import java.util.*;
import java.util.concurrent.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Object 1
// Create a ConcurrentLinkedDeque object
// Declaring object of Integer type
ConcurrentLinkedDeque dq
= new ConcurrentLinkedDeque();
// Adding element to the front
// using addFirst() method
// Custom entry
dq.add(89);
// Adding an element in the last
// using addLast() method
// Custom entry
dq.addLast(18);
// Adding an element to the front
// Custom inputs
dq.addFirst(10);
dq.add(45);
// Displaying the current ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque1 : "
+ dq);
// Object 2
// Creating a ConcurrentLinkedDeque object
// using ConcurrentLinkedDeque(Collection c)
// Declaring object of Integer type
ConcurrentLinkedDeque ldq
= new ConcurrentLinkedDeque(dq);
// Displaying the current ConcurrentLinkedDeque
System.out.println("ConcurrentLinkedDeque2 : "
+ ldq);
// Print the size of the deque
// using size() method
System.out.println("Size: " + ldq.size());
// Removing all the elements from the deque
// using clear() method
ldq.clear();
// Checking whether the ConcurrentLinkedDeque object
// is empty or not
System.out.println("Is Deque empty: "
+ ldq.isEmpty());
// Removing the head of deque of object1
dq.remove();
// Iterating over elements and
// printing deque of object1
Iterator it = dq.iterator();
// Condition check using hasNext() which hold
// true till single element remaining in List
while (it.hasNext())
// Print all the elements
System.out.print(it.next() + " ");
}
}
ConcurrentLinkedDeque1: [10, 89, 18, 45]
ConcurrentLinkedDeque2: [10, 89, 18, 45]
Size: 4
Is Deque empty: true
89 18 45