Java程序 实现ArrayBlockingQueue API
ArrayBlockingQueue类是Java Collection框架的一个成员。ArrayBlockingQueue是一个有界阻塞队列。有界的意思是,队列的大小是固定的,不能被改变。任何试图将元素/元素放入一个完整的队列的行为都将导致阻塞操作。同样地,任何试图从一个空队列中移除元素的行为都将导致阻塞操作。
ArrayBlockingQueue的这种绑定大小的特性可以通过最初在ArrayBlockingQueue的构造函数中传递容量作为参数来实现。这个队列对元素进行FIFO(先进先出)排序。这意味着元素可以在队列的尾部插入,也可以从队列的头部移除。
队列的尾部有最新的元素,而队列的头部有最旧的元素。
该类扩展了AbstractQueue
语法:
public class ArrayBlockingQueue
// 这里,E是存储在集合中的元素的类型
基本操作
- 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) – 在这个队列的尾部插入指定的元素,如果队列已满,则等待空间变得可用。
* remainingCapacity() – 返回这个队列在理想情况下(在没有内存或资源限制的情况下)可以接受的额外元素的数量,而不会阻塞。
* remove(Object o) – 从这个队列中删除一个指定元素的单一实例,如果它存在的话。
* removeAll(Collection> c) – 移除此集合中也包含在指定集合中的所有元素(可选操作)。 - removeIf(Predicate super E> filter) – 移除此集合中满足给定谓词的所有元素。
* retainAll(Collection> c) – 只保留这个集合中包含在指定集合中的元素(可选操作)。 - size() – 返回该队列中元素的数量。
- spliterator() – 返回该队列中元素的Spliterator。
- toArray() – 返回一个包含该队列中所有元素的数组,以适当的顺序排列。
- toArray(T[] a) – 返回一个包含该队列中所有元素的数组,并以适当的顺序排列;返回的数组的运行时类型是指定数组的类型。
- take() – 检索并删除这个队列的头部,如果有必要,可以等待,直到有元素可用。
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
public class ArrayBlockingQueueImpl<E> {
private ArrayBlockingQueue<E> arrayBlockingQueue;
// constructor to create object of ArrayBlockingQueue
// class with the specified capacity.
public ArrayBlockingQueueImpl(int cap)
{
arrayBlockingQueue = new ArrayBlockingQueue<E>(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<E>(
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<E> 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> 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<String> queue
= new ArrayBlockingQueue<String>(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 :[]