当前位置:编程学习 > JAVA >>

集合框架 Queue---ArrayBlockingQueue

java.util.concurrent
类 ArrayBlockingQueue<E>
java.lang.Object
  java.util.AbstractCollection<E>
      java.util.AbstractQueue<E>
          java.util.concurrent.ArrayBlockingQueue<E>
类型参数:
E - 在此 collection 中保持的元素类型
所有已实现的接口:
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
public class ArrayBlockingQueue<E>
extends AbstractQueue<E>
implements BlockingQueue<E>, Serializable
一个由数组支持的有界阻塞队列。此队列按 FIFO(先进先出)原则对元素进行排序。队列的头部 是在队列中存在时间最长的元素。队列的尾部 是在队列中存在时间最短的元素。新元素插入到队列的尾部,队列获取操作则是从队列头部开始获得元素。
这是一个典型的“有界缓存区”,固定大小的数组在其中保持生产者插入的元素和使用者提取的元素。一旦创建了这样的缓存区,就不能再增加其容量。试图向已满队列中放入元素会导致操作受阻塞;试图从空队列中提取元素将导致类似阻塞。
此类支持对等待的生产者线程和使用者线程进行排序的可选公平策略。默认情况下,不保证是这种排序。然而,通过将公平性 (fairness) 设置为 true 而构造的队列允许按照 FIFO 顺序访问线程。公平性通常会降低吞吐量,但也减少了可变性和避免了“不平衡性”。
此类及其迭代器实现了 Collection 和 Iterator 接口的所有可选 方法。
此类是 Java Collections Framework 的成员。
实现了Serializable可知道是其实线程安全的
源代码:
[java] 
public class ArrayBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, java.io.Serializable { 
    /** 队列元素 数组 */ 
    private final E[] items; 
    /** 获取、删除元素时的索引(take, poll 或 remove操作) */ 
    private int takeIndex; 
    /** 添加元素时的索引(put, offer或 add操作) */ 
    private int putIndex; 
    /** 队列元素的数目*/ 
    private int count; 
 
    /** 锁 */ 
    private final ReentrantLock lock; 
    /** 获取操作时的条件 */ 
    private final Condition notEmpty; 
    /** 插入操作时的条件 */ 
    private final Condition notFull; 
    //超出数组长度时,重设为0 
    final int inc(int i) { 
        return (++i == items.length)? 0 : i; 
    } 
    /**
     * 插入元素(在获得锁的情况下才调用)
     */ 
    private void insert(E x) { 
        items[putIndex] = x; 
        putIndex = inc(putIndex); 
        ++count; 
        notEmpty.signal(); 
    } 
    /**
     * 获取并移除元素(在获得锁的情况下才调用)
     */ 
    private E extract() { 
        final E[] items = this.items; 
        E x = items[takeIndex]; 
        items[takeIndex] = null; 
        takeIndex = inc(takeIndex);//移到下一个位置 
        --count; 
        notFull.signal(); 
        return x; 
    } 
    /**
     * 删除i位置的元素
     */ 
    void removeAt(int i) { 
        final E[] items = this.items; 
        // if removing front item, just advance 
        if (i == takeIndex) { 
            items[takeIndex] = null; 
            takeIndex = inc(takeIndex); 
        } else { 
            // 把i后面的直到putIndex的元素都向前移动一个位置 
            for (;;) { 
                int nexti = inc(i); 
                if (nexti != putIndex) { 
                    items[i] = items[nexti]; 
                    i = nexti; 
                } else { 
                    items[i] = null; 
                    putIndex = i; 
                    break; 
                } 
            } 
        } 
        --count; 
        notFull.signal(); 
    } 
    /**
     *构造方法,指定容量,默认策略(不是按照FIFO的顺序访问)
     */ 
    public ArrayBlockingQueue(int capacity) { 
        this(capacity, false); 
    } 
    /**
     *构造方法,指定容量及策略
     */ 
    public ArrayBlockingQueue(int capacity, boolean fair) { 
        if (capacity <= 0) 
            throw new IllegalArgumentException(); 
        this.items = (E[]) new Object[capacity]; 
        lock = new ReentrantLock(fair); 
        notEmpty = lock.newCondition(); 
        notFull =  lock.newCondition(); 
    } 
    /**
     * 通过集合构造
     */ 
    public ArrayBlockingQueue(int capacity, boolean fair, 
                              C
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,