📜  实现 LinkedBlockingDeque API 的Java程序

📅  最后修改于: 2022-05-13 01:54:52.219000             🧑  作者: Mango

实现 LinkedBlockingDeque API 的Java程序

LinkedBlockingDeque JDK 1.6 中引入的类存在于Java.util.concurrent包中。它是一个双端队列,意思是双端队列。 LinkedBlockingDeque 的默认大小是 Integer.MAX_VALUE。它实现 BlockingDeque 类并提供基于链接节点的可选有界功能。这种可选的有界性通过在构造函数中传递所需的大小来提供,并有助于防止内存浪费。如果该线程试图从空双端队列中删除元素,则该类会阻塞该线程。

它实现了 Serializable , Iterable , Collection , BlockingDeque, BlockingQueue, Deque, Queue 接口并扩展了 AbstractQueue 和 AbstractCollection 类。

宣言:

public class LinkedBlockingDeque extends AbstractQueue implements 
BlockingDeque, Serializable

这里, E是存储在集合中的元素类型。

LinkedBlockingDeque API 的实现:

Java
// Java program to show the implementation
// of LinkedBlockingDeque API
 
import java.util.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
 
class LinkedBlockingDequeImpl {
    private LinkedBlockingDeque linkedBlockingDeque;
 
    // constructor to create a LinkedBlockingDeque .
    public LinkedBlockingDequeImpl()
    {
        linkedBlockingDeque = new LinkedBlockingDeque();
    }
 
    // constructor to create a LinkedBlockingDeque initially
    // containing the elements of the given collection
    public LinkedBlockingDequeImpl(
        Collection collection)
    {
        linkedBlockingDeque
            = new LinkedBlockingDeque(collection);
    }
 
    // constructor to create a LinkedBlockingDeque with
    // specified capacity.
    public LinkedBlockingDequeImpl(int cap)
    {
        linkedBlockingDeque
            = new LinkedBlockingDeque(cap);
    }
 
    // method to remove all of the elements from the deque.
    public void clear() { linkedBlockingDeque.clear(); }
 
    // method to check if deque contains the specified
    // element.
    public boolean contains(Object object)
    {
        return linkedBlockingDeque.contains(object);
    }
 
    // method to remove all of the elements from the deque
    // and add them to specified collection.
    public int drainTo(Collection collection)
    {
        return linkedBlockingDeque.drainTo(collection);
    }
 
    // method to remove specified number of the elements
    // from the deque and add them to specified collection.
    public int drainTo(Collection collection,
                       int maxElement)
    {
        return linkedBlockingDeque.drainTo(collection,
                                           maxElement);
    }
 
    // method to return an iterator over the elements in
    // this deque .
    public Iterator iterator()
    {
        return linkedBlockingDeque.iterator();
    }
 
    // method to insert a specified element at the tail of
    // this queue. Return true upon successfull insertion
    // else return false.
    public boolean offer(E element)
    {
        return linkedBlockingDeque.offer(element);
    }
 
    // method to insert a specified element at the tail of
    // this queue. Return true upon successfull insertion
    // else return false.
    public boolean offer(E element, long timeout,
                         TimeUnit unit)
        throws InterruptedException
    {
        return linkedBlockingDeque.offer(element, timeout,
                                         unit);
    }
 
    // method to retrieve element from head of deque .
    public E peek() { return linkedBlockingDeque.peek(); }
 
    // method to remove and retrieve element from head of
    // deque .
    public E poll() { return linkedBlockingDeque.poll(); }
 
    // method to remove and retrieve element from head of
    // deque .
    public E poll(long timeout, TimeUnit unit)
        throws InterruptedException
    {
        return linkedBlockingDeque.poll(timeout, unit);
    }
 
    // method to insert the specified element at the tail of
    // the deque.
    public void put(E element) throws InterruptedException
    {
        linkedBlockingDeque.put(element);
    }
 
    // method to return the number of additional elements
    // that the deque can ideally accept without blocking.
    public int remainingCapacity()
    {
        return linkedBlockingDeque.remainingCapacity();
    }
 
    // method to remove a single instance of the
    // specified element from the deque
    public boolean remove(Object object)
    {
        return linkedBlockingDeque.remove(object);
    }
 
    // method to return the number of elements in the deque.
    public int size() { return linkedBlockingDeque.size(); }
 
    // method to retrieve and remove the head of the deque.
    public E take() throws InterruptedException
    {
        return linkedBlockingDeque.take();
    }
 
    // method to return an array containing all of
    // the elements in the deque.
    public Object[] toArray()
    {
        return linkedBlockingDeque.toArray();
    }
 
    // method to return an array containing all of
    // the elements in the deque
    public  T[] toArray(T[] array)
    {
        return linkedBlockingDeque.toArray(array);
    }
 
    // method to return a string representation of the
    // collection
    public String toString()
    {
        return linkedBlockingDeque.toString();
    }
 
    // method to insert specified element at the front of
    // the deque.
    public void addFirst(E element)
    {
        linkedBlockingDeque.addFirst(element);
    }
 
    // method to insert specified element at the end of the
    // deque.
    public void addLast(E element)
    {
        linkedBlockingDeque.addLast(element);
    }
 
    // method to retrieve first element of the deque.
    public void getFirst()
    {
        linkedBlockingDeque.getFirst();
    }
 
    // method to retrieve the last element of the deque.
    public void getLast() { linkedBlockingDeque.getLast(); }
 
    // Inserts the specified element at the front of this
    // deque
    public boolean offerFirst(E element)
    {
        return linkedBlockingDeque.offerFirst(element);
    }
 
    // method to insert a specified element at the end of
    // the deque.
    public boolean offerLast(E element)
    {
        return linkedBlockingDeque.offerLast(element);
    }
 
    // method to retrieve first element of the deque .
    public E peekFirst()
    {
        return linkedBlockingDeque.peekFirst();
    }
 
    // method to retrieve last element of the deque .
    public E peekLast()
    {
        return linkedBlockingDeque.peekLast();
    }
}
 
public class LinkedBlockingDequeDemo {
    public static void main(String[] args)
        throws InterruptedException
    {
       
        // create object of LinkedBlockingDeque
        // using LinkedBlockingDeque() constructor
        LinkedBlockingDequeImpl linkedBlockingDeque
            = new LinkedBlockingDequeImpl();
       
        try {
           
            // Add numbers to end of LinkedBlockingDeque
            linkedBlockingDeque.put(10);
            linkedBlockingDeque.put(20);
            linkedBlockingDeque.put(30);
        }
       
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
       
        System.out.println(
            "the elements of the linkedBlockingDeque is ");
       
        Iterator itr = linkedBlockingDeque.iterator();
       
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
       
        System.out.println();
       
        linkedBlockingDeque.offer(60);
        linkedBlockingDeque.offer(70);
       
        System.out.println(
            "the peak element of the linkedBlockingDeque is(by peeking) "
            + linkedBlockingDeque.peek());
       
        System.out.println(
            "the peak element of the linkedBlockingDeque is(by polling) "
            + linkedBlockingDeque.poll());
       
        System.out.println(
            "the remaining capacity is "
            + linkedBlockingDeque.remainingCapacity());
       
        System.out.println(
            "element 30 removed "
            + linkedBlockingDeque.remove(30));
       
        System.out.println(
            "the linkedBlockingDeque contains 40 :"
            + linkedBlockingDeque.contains(40));
       
        System.out.println(
            "the linkedBlockingDeque contains 10 :"
            + linkedBlockingDeque.contains(10));
       
        System.out.println(
            "the size of the linkedBlockingDeque is "
            + linkedBlockingDeque.size());
       
        System.out.println(linkedBlockingDeque);
    }
}


输出
the elements of the linkedBlockingDeque is 
10    20    30    
the peak element of the linkedBlockingDeque is(by peeking) 10
the peak element of the linkedBlockingDeque is(by polling) 10
the remaining capacity is 2147483643
element 30 removed true
the linkedBlockingDeque contains 40 :false
the linkedBlockingDeque contains 10 :false
the size of the linkedBlockingDeque is 3
[20, 60, 70]