Java程序 实现LinkedBlockingQueue API

Java程序 实现LinkedBlockingQueue API

LinkedBlockingQueue API是一个基于链接节点的可选边界的队列。它以FIFO(先进先出)的顺序排列元素。这个队列的头部是在队列中存在时间最长的元素,队列的尾部是在队列中存在时间最短的元素。新的元素在队列的尾部插入,而队列的检索操作则在队列的头部获得元素。它属于java.util.concurrent包。它扩展了Object、AbstractCollection和AbstractQueue类。

容量参数的作用是防止队列过度扩张。容量(如果没有指定)等于Integer.MAX_VALUE。链接节点在每次插入时被动态创建,除非这将使队列超过容量。

LinkedBlockingQueue API是Java Collection Framework的一个成员。

所有实现的接口。

1. Serializable
2. Iterable<E>
3. Collection<E>
4. BlockingQueue<E>
5. Queue<E>

参数: E – 集合中元素的类型

语法:

public class LinkedBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, Serializable

构造函数:

  1. public LinkedBlockingQueue() – 创建一个容量为Integer.MAX_VALUE的LinkedBlockingQueue。
    1. public LinkedBlockingQueue(Collection<? extends E> c) – 创建一个容量为Integer.MAX_VALUE的LinkedBlockingQueue,最初包含指定Collection的元素。
    2. public LinkedBlockingQueue(int capacity) – 创建一个具有给定容量的LinkedBlockingQueue。

实现:

示例

// Java Program to Implement LinkedBlockingQueue API
 
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class BlockingQueue<E> {
    private LinkedBlockingQueue<E> q;
 
    // Creates a LinkedBlockingQueue with a size of
    // Integer.MAX_VALUE.
    public BlockingQueue()
    {
        q = new LinkedBlockingQueue<E>();
    }
 
    // Creates a LinkedBlockingQueue initially containing
    // the elements of the given collection
    public BlockingQueue(Collection<? extends E> c)
    {
        q = new LinkedBlockingQueue<E>(c);
    }
 
    // Creates a LinkedBlockingQueue with the given size
    public BlockingQueue(int size)
    {
        q = new LinkedBlockingQueue<E>(size);
    }
 
    // Returns true if the queue contains the given element
    public boolean contains(Object o)
    {
        return q.contains(o);
    }
 
    // Removes all elements from the queue and adds them to
    // the given collection.
    public int drainTo(Collection<? super E> c)
    {
        return q.drainTo(c);
    }
 
    //  Removes the elements from the queue and adds them to
    //  the given collection.
    public int drainTo(Collection<? super E> c, int maxEle)
    {
        return q.drainTo(c, maxEle);
    }
 
    // Returns an iterator over the elements in the queue
    public Iterator<E> iterator() { return q.iterator(); }
 
    // removes all the elements from the queue.
    void clear() { q.clear(); }
 
    // Inserts the given element at the end of the queue,
    // returning true upon inserting and false if the queue
    // is full.
    public boolean offer(E e) { return q.offer(e); }
 
    // inserts then given element at the end and wait for
    // the given time for the space to become available
    public boolean offer(E e, long timeout, TimeUnit unit)
        throws InterruptedException
    {
        return q.offer(e, timeout, unit);
    }
 
    // Retrieves, but does not remove, the head of the
    // queue, or returns null if this queue is empty.
    public E peek() { return q.peek(); }
 
    //  Retrieves and removes the head of the queue
    public E poll() { return q.poll(); }
 
    // Retrieves and removes the head of the queue and wait
    // for the specified time for the element to become
    // available.
    public E poll(long tout, TimeUnit un)
        throws InterruptedException
    {
        return q.poll(tout, un);
    }
 
    // Returns the number of additional elements that the
    // queue can contain without blocking.
    public int remainingCapacity()
    {
        return q.remainingCapacity();
    }
 
    // Removes the specified element from the queue
    public boolean remove(Object o) { return q.remove(o); }
 
    // Returns the number of elements in the queue
    public int size() { return q.size(); }
 
    // Inserts the given element at the end of the queue,
    // wait for space to become available
    public void put(E e) throws InterruptedException
    {
        q.put(e);
    }
 
    // Retrieves and removes the head of the queue wait till
    // an element becomes available
    public E take() throws InterruptedException
    {
        return q.take();
    }
 
    // Returns an array containing all the elements in the
    // queue.
    public Object[] toArray() { return q.toArray(); }
 
    // Returns an array containing all of the elements in
    // the queue
    public <T> T[] toArray(T[] a) { return q.toArray(a); }
 
    // Returns a string representation of th collection.
    public String toString() { return q.toString(); }
 
    public static void main(String[] args)
    {
        BlockingQueue<Integer> q
            = new BlockingQueue<Integer>();
        try {
            q.put(1);
            q.put(2);
            q.put(3);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        System.out.println(
            "The elements of the LinkedBlockingQueue is ");
 
        Iterator<Integer> i = q.iterator();
 
        while (i.hasNext()) {
            System.out.print(i.next() + "\t");
        }
        System.out.println();
 
        System.out.println("The remaining capacity is "
                           + q.remainingCapacity());
 
        System.out.println("3 removed " + q.remove(3));
 
        q.offer(6);
        q.offer(7);
 
        System.out.println(
            "The peak element of the LinkedBlockingQueue is "
            + q.peek());
 
        System.out.println(
            "The peak element of the LinkedBlockingQueue is "
            + q.poll());
 
        System.out.println(
            "The LinkedBlockingQueue contains 4 :"
            + q.contains(4));
 
        System.out.println(
            "The LinkedBlockingQueue contains 1 :"
            + q.contains(1));
 
        System.out.println(
            "The size of the LinkedBlockingQueue is "
            + q.size());
 
        System.out.println(q);
    }
}

输出

The elements of the LinkedBlockingQueue is 
1    2    3    
The remaining capacity is 2147483644
3 removed true
The peak element of the LinkedBlockingQueue is 1
The peak element of the LinkedBlockingQueue is 1
The LinkedBlockingQueue contains 4 :false
The LinkedBlockingQueue contains 1 :false
The size of the LinkedBlockingQueue is 3
[2, 6, 7]

方法:

方法 类型 描述
clear() void 移除LinkedBlockingQueue的所有元素。
contains(Object O) boolean 如果队列包含指定的元素,返回真。
iterator() Iterator() 返回队列中元素的一个迭代器。
offer(E e) boolean 如果可以的话,在这个队列的尾部插入指定的元素,成功后返回true,否则返回false。
offer(E e, long timeout, TimeUnit unit) boolean 在队列的尾部插入指定的元素,并等待指定的时间,使空间变得可用。
peek() E 检索,但不删除队列的头部。
poll() E 检索并删除队列的头部。
put(E e) void 在这个队列的尾部插入指定的元素,如果需要的话,等待空间的出现。
remainingCapacity() int 返回这个队列在理想情况下可以接受的额外元素的数量,而不会出现阻塞。
size() int 返回队列的大小
toArray() Object[] 将队列转换为一个数组
drainTo(Collection<? super E> c) int 从队列中移除所有可用的元素并将它们添加到指定的集合中。
take() E 检索并删除这个队列的头部,如果有必要,可以等待,直到有元素可用。
remove(Object O) boolean 从队列中删除指定的元素,如果它存在的话。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程