实现ArrayBlockingQueue API的Java程序
ArrayBlockingQueue 类是Java Collection 框架的成员。 ArrayBlockingQueue 是一个有界阻塞队列。术语有界,意味着队列的大小是固定的,不能改变。任何将元素放入一个完整队列的尝试都会导致阻塞操作。类似地,任何从空队列中移除元素的尝试都会导致阻塞操作。
ArrayBlockingQueue 的这个 bound size 特性可以通过在 ArrayBlockingQueue 的构造函数中初始传递容量作为参数来实现。该队列对元素 FIFO(先进先出)进行排序。这意味着元素可以插入到队列的尾部,也可以从队列的头部移除。
队列的尾部具有最新的元素,而队列的头部具有最旧的元素。
此类扩展 AbstractQueue
句法:
public class ArrayBlockingQueue
// Here, E is the type of elements stored in the collection
基本操作
- add (E e):如果可以在不超过队列容量的情况下立即将指定元素插入该队列的尾部,则在成功时返回 true,如果该队列已满则抛出 IllegalStateException。
- clear():从这个队列中原子地删除所有元素。
- contains (Object o) – 如果此队列包含指定元素,则返回 true。
- DrainTo (Collection super E> c) – 从此队列中移除所有可用元素并将它们添加到给定的集合中。
- DrainTo (Collection super E> c, int maxElements) – 最多从此队列中移除给定数量的可用元素并将它们添加到给定的集合中。
- forEach (Consumer super E> action) – 对 Iterable 的每个元素执行给定的动作,直到所有元素都被处理或动作抛出异常。
- iterator() – 以适当的顺序返回此队列中元素的迭代器。
- offer (E e) – 如果可以在不超过队列容量的情况下立即将指定元素插入到此队列的尾部,则在成功时返回 true,如果此队列已满则返回 false。
- offer (E e, long timeout, TimeUnit unit) – 在此队列的尾部插入指定的元素,如果队列已满,则等待指定的等待时间让空间变为可用。
- put (E e) – 在此队列的尾部插入指定的元素,如果队列已满,则等待空间变得可用。
- 剩余容量() - 返回此队列在理想情况下(在没有内存或资源限制的情况下)可以接受而不会阻塞的附加元素的数量。
- remove (Object o) – 从此队列中移除指定元素的单个实例(如果存在)。
- removeAll (Collection> c) – 移除所有也包含在指定集合中的集合元素(可选操作)。
- removeIf (Predicate super E> filter) – 移除该集合中满足给定谓词的所有元素。
- retainAll (Collection> c) – 仅保留此集合中包含在指定集合中的元素(可选操作)。
- size() – 返回此队列中的元素数。
- spliterator() – 在此队列中的元素上返回一个 Spliterator。
- toArray() – 以正确的顺序返回一个包含此队列中所有元素的数组。
- toArray (T[] a) – 以适当的顺序返回一个包含此队列中所有元素的数组;返回数组的运行时类型是指定数组的类型。
- take() – 检索并移除此队列的头部,必要时等待元素变为可用。
Java
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
public class ArrayBlockingQueueImpl {
private ArrayBlockingQueue arrayBlockingQueue;
// constructor to create object of ArrayBlockingQueue
// class with the specified capacity.
public ArrayBlockingQueueImpl(int cap)
{
arrayBlockingQueue = new ArrayBlockingQueue(cap);
}
// constructor to create object of ArrayBlockingQueue
// class with the specified capacity and specified
// access policy.
public ArrayBlockingQueueImpl(int cap, boolean fair)
{
arrayBlockingQueue
= new ArrayBlockingQueue<>(cap, fair);
}
// constructor to create object of ArrayBlockingQueue
// class with the specified capacity and specified
// access initially containing the elements of the given
// collection.
public ArrayBlockingQueueImpl(
int cap, boolean fair,
Collection extends E> collection)
{
arrayBlockingQueue = new ArrayBlockingQueue(
cap, fair, collection);
}
// method used to append a element to queue's
// tail.Return true upon successful operation else throw
// IllegalStateException if this queue reached capacity.
boolean add(E e) { return arrayBlockingQueue.add(e); }
// method used to removes all elements from the queue
void clear() { arrayBlockingQueue.clear(); }
// method used to check it queue contains specified
// element.
public boolean contains(Object o)
{
return arrayBlockingQueue.contains(o);
}
// method used clear the queue and add all the elements
// to specified collection.
public int drainTo(Collection super E> c)
{
return arrayBlockingQueue.drainTo(c);
}
// method used to remove at most specified number of
// elements from the queue and adds them to the
// specified collection.
public int drainTo(Collection super E> c,
int maxElements)
{
return arrayBlockingQueue.drainTo(c, maxElements);
}
// method used to return an iterator over the elements
// in the queue.This iterator could be used for
// traversing the element of queue.
public Iterator iterator()
{
return arrayBlockingQueue.iterator();
}
// method used to append specified element to queue's
// tail.Return true upon successful operation else
// return false.
public boolean offer(E e)
{
return arrayBlockingQueue.offer(e);
}
// method used to insert the specified element at the
// tail of the queue
public boolean offer(E e, long timeout, TimeUnit unit)
throws InterruptedException
{
return arrayBlockingQueue.offer(e, timeout, unit);
}
// method used to get head of queue.Return NULL in case
// of empty queue.
public E peek() { return arrayBlockingQueue.peek(); }
// method used to remove head of the queue.Returns NULL
// if queue is empty else returns removed element.
public E poll() { return arrayBlockingQueue.poll(); }
// method used to remove head of this queue, waiting up
// to the specified wait time if necessary for an
// element to become available
public E poll(long timeout, TimeUnit unit)
throws InterruptedException
{
return arrayBlockingQueue.poll(timeout, unit);
}
// method used to inserts the specified element at
// queue's tail.
public void put(E e) throws InterruptedException
{
arrayBlockingQueue.put(e);
}
// method used to return number of additional elements
// that the queue can have without blocking.
public int remainingCapacity()
{
return arrayBlockingQueue.remainingCapacity();
}
// method used to remove single instance of the
// specified element from this queue
public boolean remove(Object o)
{
return arrayBlockingQueue.remove(o);
}
// method used to return size of queue.
public int size() { return arrayBlockingQueue.size(); }
// method used remove head of queue.Return NULL if queue
// is empty else return removed element.
public E take() throws InterruptedException
{
return arrayBlockingQueue.take();
}
// method used to return an array of the elements in
// this queue.
public Object[] toArray()
{
return arrayBlockingQueue.toArray();
}
// method used to return an array of the elements in
// this queue.
public T[] toArray(T[] a)
{
return arrayBlockingQueue.toArray(a);
}
// method used to return string representation queue.
public String toString()
{
return arrayBlockingQueue.toString();
}
public static void main(String[] args)
{
// capacity of ArrayBlockingQueue
int capacity_queue = 10;
// fair value of queue.If fair value is if true then
// queue accesses for threads blocked on insertion or
// removal, are processed in FIFO order if false then
// access order is unspecified.
boolean fair = true;
// create object of ArrayBlockingQueue
ArrayBlockingQueue queue
= new ArrayBlockingQueue(capacity_queue,
fair);
// add element to the queue
queue.add("one");
queue.add("two");
queue.add("three");
queue.add("four");
queue.add("five");
// print queue
System.out.println(
"ArrayBlockingQueue (when fair policy is true):"
+ queue);
// print head element of queue
System.out.println("Peek element of the queue : "
+ queue.peek());
// delete the specified element from the queue
System.out.println(
"Deleting the element 'five' from the queue : "
+ queue.remove("five"));
// print queue
System.out.println("ArrayBlockingQueue :" + queue);
// clear the queue
queue.clear();
// print queue
System.out.println(
"ArrayBlockingQueue after clear operation :"
+ queue);
}
}
输出
ArrayBlockingQueue (when fair policy is true):[one, two, three, four, five]
Peek element of the queue : one
Deleting the element 'five' from the queue : true
ArrayBlockingQueue :[one, two, three, four]
ArrayBlockingQueue after clear operation :[]