Java中的 LinkedBlockingDeque 示例
Java中的LinkedBlockingDeque类是Java集合框架的一部分。它是在 JDK 1.6 中引入的,属于Java.util.concurrent包。它是一个双端队列(双端队列),如果该线程在双端队列为空时尝试从中取出元素,则会阻塞线程。它实现了 BlockingDeque 并提供了基于链接节点的可选有界功能。通过在构造函数中传递所需的大小来提供这种可选的有界性,并有助于防止内存浪费。未指定时,容量默认为Integer.MAX_VALUE。此类及其迭代器实现了Collection和Iterator接口的所有可选方法。 LinkedBlockingDeque 提供的实现是线程安全的。类中的所有排队方法在内部使用 ReentrantLock 以原子方式实现其效果。
LinkedBlockingDeque 的层次结构
它实现了 Serializable 、 Iterable
宣言:
public class LinkedBlockingDeque
这里, E是存储在集合中的元素的类型。
Java LinkedBlockingDeque 中的构造函数
为了创建 LinkedBlockingDeque 的实例,我们需要从Java.util.concurrent包中导入它。
1. LinkedBlockingDeque() :该构造函数用于构造一个空的双端队列。在这种情况下,容量设置为 Integer.MAX_VALUE。
LinkedBlockingDeque
2. LinkedBlockingDeque(int capacity) :此构造函数创建一个具有给定(固定)容量的 LinkedBlockingDeque。
LinkedBlockingDeque
3. LinkedBlockingDeque(Collection
LinkedBlockingDeque
下面是一个示例程序来说明Java中的 LinkedBlockingDeque:示例 1:
Java
// Java Program Demonstrate LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class LinkedBlockingDequeDemo {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedBlockingDeque
// using LinkedBlockingDeque() constructor
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
LBD.add(7855642);
LBD.add(35658786);
LBD.add(5278367);
LBD.add(74381793);
// print Dequee
System.out.println("Linked Blocking Deque1: "
+ LBD);
System.out.println("Size of Linked Blocking Deque1: "
+ LBD.size());
// create object of LinkedBlockingDeque
// using LinkedBlockingDeque(int capacity) constructor
LinkedBlockingDeque LBD1
= new LinkedBlockingDeque(3);
// Add numbers to end of LinkedBlockingDeque
LBD1.add(7855642);
LBD1.add(35658786);
LBD1.add(5278367);
try {
// adding the 4th element
// will throw exception for Deque full
LBD1.add(74381793);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
// print Dequee
System.out.println("Linked Blocking Deque2: "
+ LBD1);
System.out.println("Size of Linked Blocking Deque2: "
+ LBD1.size());
// create object of LinkedBlockingDeque
// using LinkedBlockingDeque(Collection c) constructor
LinkedBlockingDeque LBD2
= new LinkedBlockingDeque(LBD1);
// print Dequee
System.out.println("Linked Blocking Deque3: "
+ LBD2);
}
}
Java
// Java code to illustrate methods of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class LinkedBlockingDequeDemo {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
// using add() method
LBD.add(7855642);
LBD.add(35658786);
LBD.add(5278367);
LBD.add(74381793);
// prints the Deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// prints the size of Deque after removal
// using size() method
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
// removes the front element and prints it
// using removeFirst() method
System.out.println("First element: "
+ LBD.removeFirst());
// prints the Deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// prints the size of Deque after removal
// using size() method
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
// Add numbers to end of LinkedBlockingDeque
// using offer() method
LBD.offer(20);
// prints the Deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// prints the size of Deque after removal
// using size() method
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
}
}
Java
// Java Program Demonstrate adding
// elements to LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class AddingElementsExample {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque lbd
= new LinkedBlockingDeque();
// Add number to end of LinkedBlockingDeque
lbd.add(7855642);
// Add integer at the head or front
lbd.addFirst(35658786);
// Add integer at the tail or end
lbd.addLast(5278367);
// print Deque
System.out.println("Linked Blocking Deque: " + lbd);
// Create object of ArrayList collection
ArrayList ArrLis
= new ArrayList();
// Add number to ArrayList
ArrLis.add(55);
ArrLis.add(66);
ArrLis.add(77);
ArrLis.add(88);
// Print ArrayList
System.out.println("ArrayList: " + ArrLis);
// Function addAll() adds all the elements of
// ArrayList to Deque
lbd.addAll(ArrLis);
// Print deque
System.out.println("Linked Blocking Deque: " + lbd);
}
}
Java
// Java Program Demonstrate removing
// elements of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class RemovingElementsExample {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque lbd
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
lbd.add(7855642);
lbd.add(35658786);
lbd.add(5278367);
lbd.add(74381793);
lbd.add(12345566);
// print Dequeue
System.out.println("Linked Blocking Deque: " + lbd);
// removes the front element
lbd.remove();
// print the modified deque
System.out.println("Linked Blocking Deque: " + lbd);
// removes the front element
lbd.removeFirst();
// print the modified deque
System.out.println("Linked Blocking Deque: " + lbd);
// removes the last element
lbd.removeLast();
// print the modified deque
System.out.println("Linked Blocking Deque: " + lbd);
}
}
Java
// Java Program Demonstrate iterating
// over LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class IteratingExample {
public static void main(String[] args)
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to front of LinkedBlockingDeque
LBD.addFirst(7855642);
LBD.addFirst(35658786);
LBD.addFirst(5278367);
LBD.addFirst(74381793);
// Call iterator() method of LinkedBlockingDeque
Iterator iteratorVals = LBD.iterator();
// Print elements of iterator
// created from PriorityBlockingQueue
System.out.println("The iterator values"
+ " of LinkedBlockingDeque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
Linked Blocking Deque1: [7855642, 35658786, 5278367, 74381793]
Size of Linked Blocking Deque1: 4
Exception: java.lang.IllegalStateException: Deque full
Linked Blocking Deque2: [7855642, 35658786, 5278367]
Size of Linked Blocking Deque2: 3
Linked Blocking Deque3: [7855642, 35658786, 5278367]
示例 2:
Java
// Java code to illustrate methods of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class LinkedBlockingDequeDemo {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
// using add() method
LBD.add(7855642);
LBD.add(35658786);
LBD.add(5278367);
LBD.add(74381793);
// prints the Deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// prints the size of Deque after removal
// using size() method
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
// removes the front element and prints it
// using removeFirst() method
System.out.println("First element: "
+ LBD.removeFirst());
// prints the Deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// prints the size of Deque after removal
// using size() method
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
// Add numbers to end of LinkedBlockingDeque
// using offer() method
LBD.offer(20);
// prints the Deque
System.out.println("Linked Blocking Deque: "
+ LBD);
// prints the size of Deque after removal
// using size() method
System.out.println("Size of Linked Blocking Deque: "
+ LBD.size());
}
}
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Size of Linked Blocking Deque: 4
First element: 7855642
Linked Blocking Deque: [35658786, 5278367, 74381793]
Size of Linked Blocking Deque: 3
Linked Blocking Deque: [35658786, 5278367, 74381793, 20]
Size of Linked Blocking Deque: 4
基本操作
1.添加元素
LinkedBlockingDeque 提供了多种方法来在两端添加或插入元素。它们是 add(E e)、addAll(Collection c)、addFirst(E e)、addLast(E e) 等。
Java
// Java Program Demonstrate adding
// elements to LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class AddingElementsExample {
public static void main(String[] args)
throws IllegalStateException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque lbd
= new LinkedBlockingDeque();
// Add number to end of LinkedBlockingDeque
lbd.add(7855642);
// Add integer at the head or front
lbd.addFirst(35658786);
// Add integer at the tail or end
lbd.addLast(5278367);
// print Deque
System.out.println("Linked Blocking Deque: " + lbd);
// Create object of ArrayList collection
ArrayList ArrLis
= new ArrayList();
// Add number to ArrayList
ArrLis.add(55);
ArrLis.add(66);
ArrLis.add(77);
ArrLis.add(88);
// Print ArrayList
System.out.println("ArrayList: " + ArrLis);
// Function addAll() adds all the elements of
// ArrayList to Deque
lbd.addAll(ArrLis);
// Print deque
System.out.println("Linked Blocking Deque: " + lbd);
}
}
Linked Blocking Deque: [35658786, 7855642, 5278367]
ArrayList: [55, 66, 77, 88]
Linked Blocking Deque: [35658786, 7855642, 5278367, 55, 66, 77, 88]
2. 移除元素
LinkedBlockingDeque 提供了多种方法来从任一端删除或移除元素。它们是 remove()、removeFirst()、removeLast() 等。
Java
// Java Program Demonstrate removing
// elements of LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class RemovingElementsExample {
public static void main(String[] args)
throws InterruptedException
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque lbd
= new LinkedBlockingDeque();
// Add numbers to end of LinkedBlockingDeque
lbd.add(7855642);
lbd.add(35658786);
lbd.add(5278367);
lbd.add(74381793);
lbd.add(12345566);
// print Dequeue
System.out.println("Linked Blocking Deque: " + lbd);
// removes the front element
lbd.remove();
// print the modified deque
System.out.println("Linked Blocking Deque: " + lbd);
// removes the front element
lbd.removeFirst();
// print the modified deque
System.out.println("Linked Blocking Deque: " + lbd);
// removes the last element
lbd.removeLast();
// print the modified deque
System.out.println("Linked Blocking Deque: " + lbd);
}
}
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793, 12345566]
Linked Blocking Deque: [35658786, 5278367, 74381793, 12345566]
Linked Blocking Deque: [5278367, 74381793, 12345566]
Linked Blocking Deque: [5278367, 74381793]
3. 迭代
LinkedBlockingDeque 的 iterator() 方法以适当的顺序返回此双端队列中元素的迭代器。元素将按从第一个(头)到最后一个(尾)的顺序返回。返回的迭代器是一个“弱一致”的迭代器。
Java
// Java Program Demonstrate iterating
// over LinkedBlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class IteratingExample {
public static void main(String[] args)
{
// create object of LinkedBlockingDeque
LinkedBlockingDeque LBD
= new LinkedBlockingDeque();
// Add numbers to front of LinkedBlockingDeque
LBD.addFirst(7855642);
LBD.addFirst(35658786);
LBD.addFirst(5278367);
LBD.addFirst(74381793);
// Call iterator() method of LinkedBlockingDeque
Iterator iteratorVals = LBD.iterator();
// Print elements of iterator
// created from PriorityBlockingQueue
System.out.println("The iterator values"
+ " of LinkedBlockingDeque are:");
// prints the elements using an iterator
while (iteratorVals.hasNext()) {
System.out.println(iteratorVals.next());
}
}
}
The iterator values of LinkedBlockingDeque are:
74381793
5278367
35658786
7855642
LinkedBlockingDeque 的方法
METHOD | DESCRIPTION |
---|---|
add(E e) | Inserts the specified element at the end of this deque unless it would violate capacity restrictions. |
addAll(Collection extends E> c) | Appends all of the elements in the specified collection to the end of this deque, in the order that they are returned by the specified collection’s iterator. |
addFirst(E e) | Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. |
addLast(E e) | Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. |
clear() | Atomically removes all of the elements from this deque. |
contains(Object o) | Returns true if this deque contains the specified element. |
descendingIterator() | Returns an iterator over the elements in this deque in reverse sequential order. |
drainTo(Collection super E> c) | Removes all available elements from this queue and adds them to the given collection. |
drainTo(Collection super E> c, int maxElements) | Removes at most the given number of available elements from this queue and adds them to the given collection. |
element() | Retrieves, but does not remove, the head of the queue represented by this deque. |
forEach(Consumer super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
getFirst() | Retrieves, but does not remove, the first element of this deque. |
getLast() | Retrieves, but does not remove, the last element of this deque. |
iterator() | Returns an iterator over the elements in this deque in the proper sequence. |
offer(E e) | Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. |
offer(E e, long timeout, TimeUnit unit) | Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available. |
offerFirst(E e) | Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. |
offerFirst(E e, long timeout, TimeUnit unit) | Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available. |
offerLast(E e) | Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. |
offerLast(E e, long timeout, TimeUnit unit) | Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available. |
pop() | Pops an element from the stack represented by this deque. |
push(E e) | Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available. |
put(E e) | Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available. |
putFirst(E e) | Inserts the specified element at the front of this deque, waiting if necessary for space to become available. |
putLast(E e) | Inserts the specified element at the end of this deque, waiting if necessary for space to become available. |
remainingCapacity() | Returns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking. |
remove() | Retrieves and removes the head of the queue represented by this deque. |
remove(Object o) | Removes the first occurrence of the specified element from this deque. |
removeAll(Collection> c) | Removes all of this collection’s elements that are also contained in the specified collection (optional operation). |
removeFirst() | Retrieves and removes the first element of this deque. |
removeIf(Predicate super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
removeLast() | Retrieves and removes the last element of this deque. |
retainAll(Collection> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
size() | Returns the number of elements in this deque. |
spliterator() | Returns a Spliterator over the elements in this deque. |
toArray() | Returns an array containing all of the elements in this deque, in proper sequence (from first to last element). |
toArray(T[] a) | Returns an array containing all of the elements in this deque, in proper sequence; the runtime type of the returned array is that of the specified array. |
在类Java.util.AbstractCollection 中声明的方法
METHOD | DESCRIPTION |
---|---|
containsAll(Collection> c) | Returns true if this collection contains all of the elements in the specified collection. |
isEmpty() | Returns true if this collection contains no elements. |
toString() | Returns a string representation of this collection. |
在接口Java.util.concurrent.BlockingDeque 中声明的方法
METHOD | DESCRIPTION |
---|---|
peek() | Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty. |
poll() | Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty. |
poll(long timeout, TimeUnit unit) | Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available. |
pollFirst(long timeout, TimeUnit unit) | Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available. |
pollLast(long timeout, TimeUnit unit) | Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available. |
removeFirstOccurrence(Object o) | Removes the first occurrence of the specified element from this deque. |
removeLastOccurrence(Object o) | Removes the last occurrence of the specified element from this deque. |
take() | Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available. |
takeFirst() | Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available. |
takeLast() | Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available. |
在接口Java.util.Collection 中声明的方法
METHOD | DESCRIPTION |
---|---|
containsAll(Collection> c) | Returns true if this collection contains all of the elements in the specified collection. |
equals(Object o) | Compares the specified object with this collection for equality. |
hashCode() | Returns the hash code value for this collection. |
isEmpty() | Returns true if this collection contains no elements. |
parallelStream() | Returns a possibly parallel Stream with this collection as its source. |
stream() | Returns a sequential Stream with this collection as its source. |
toArray(IntFunction | Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array. |
在接口Java .util.Deque 中声明的方法
METHOD | DESCRIPTION |
---|---|
peekFirst() | Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. |
peekLast() | Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. |
pollFirst() | Retrieves and removes the first element of this deque, or returns null if this deque is empty. |
pollLast() | Retrieves and removes the last element of this deque, or returns null if this deque is empty. |
参考: Java : Java