您好,欢迎访问一九零五行业门户网

java集合框架 arrayblockingqueue应用分析

queue
------------
1.arraydeque, (数组双端队列)
2.priorityqueue, (优先级队列)
3.concurrentlinkedqueue, (基于链表的并发队列)
4.delayqueue, (延期阻塞队列)(阻塞队列实现了blockingqueue接口)
5.arrayblockingqueue, (基于数组的并发阻塞队列)
6.linkedblockingqueue, (基于链表的fifo阻塞队列)
7.linkedblockingdeque, (基于链表的fifo双端阻塞队列)
8.priorityblockingqueue, (带优先级的无界阻塞队列)
9.synchronousqueue (并发同步阻塞队列)
-----------------------------------------------------
arrayblockingqueue
是一个由数组支持的有界阻塞队列。此队列按 fifo(先进先出)原则对元素进行排序。队列的头部 是在队列中存在时间最长的元素。队列的尾部 是在队列中存在时间最短的元素。新元素插入到队列的尾部,队列获取操作则是从队列头部开始获得元素。
这是一个典型的“有界缓存区”,固定大小的数组在其中保持生产者插入的元素和使用者提取的元素。一旦创建了这样的缓存区,就不能再增加其容量。试图向已满队列中放入元素会导致操作受阻塞;试图从空队列中提取元素将导致类似阻塞。
此类支持对等待的生产者线程和消费者线程进行排序的可选公平策略。默认情况下,不保证是这种排序。然而,通过将公平性 (fairness) 设置为 true 而构造的队列允许按照 fifo 顺序访问线程。公平性通常会降低吞吐量,但也减少了可变性和避免了“不平衡性”。
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, collection<? extends e> c) { this(capacity, fair); if (capacity < c.size()) throw new illegalargumentexception(); for (iterator<? extends e> it = c.iterator(); it.hasnext();) add(it.next()); } /** * 插入元素到队尾(super调用offer方法) * public boolean add(e e) { * if (offer(e)) * return true; * else * throw new illegalstateexception("queue full"); * } * 将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量), * 在成功时返回 true,如果此队列已满,则抛出 illegalstateexception。 */ public boolean add(e e) { return super.add(e); } /** * 将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量), * 在成功时返回 true,如果此队列已满,则返回 false。 */ public boolean offer(e e) { if (e == null) throw new nullpointerexception(); final reentrantlock lock = this.lock; lock.lock(); try { if (count == items.length) return false; else { insert(e); return true; } } finally { lock.unlock(); } } /** * 将指定的元素插入此队列的尾部,如果该队列已满,则等待可用的空间。 */ public void put(e e) throws interruptedexception { if (e == null) throw new nullpointerexception(); final e[] items = this.items; final reentrantlock lock = this.lock; lock.lockinterruptibly(); try { try { while (count == items.length) notfull.await(); } catch (interruptedexception ie) { notfull.signal(); // propagate to non-interrupted thread throw ie; } insert(e); } finally { lock.unlock(); } } /** * 将指定的元素插入此队列的尾部,如果该队列已满,则在到达指定的等待时间之前等待可用的空间。 */ public boolean offer(e e, long timeout, timeunit unit) throws interruptedexception { if (e == null) throw new nullpointerexception(); long nanos = unit.tonanos(timeout); final reentrantlock lock = this.lock; lock.lockinterruptibly(); try { for (;;) { if (count != items.length) { insert(e); return true; } if (nanos <= 0)//如果时间到了就返回 return false; try { nanos = notfull.awaitnanos(nanos); } catch (interruptedexception ie) { notfull.signal(); // propagate to non-interrupted thread throw ie; } } } finally { lock.unlock(); } } //获取并移除此队列的头,如果此队列为空,则返回 null。 public e poll() { final reentrantlock lock = this.lock; lock.lock(); try { if (count == 0) return null; e x = extract(); return x; } finally { lock.unlock(); } } //获取并移除此队列的头部,在元素变得可用之前一直等待(如果有必要)。 public e take() throws interruptedexception { final reentrantlock lock = this.lock; lock.lockinterruptibly(); try { try { while (count == 0) notempty.await(); } catch (interruptedexception ie) { notempty.signal(); // propagate to non-interrupted thread throw ie; } e x = extract(); return x; } finally { lock.unlock(); } } //获取并移除此队列的头部,在指定的等待时间前等待可用的元素(如果有必要)。 public e poll(long timeout, timeunit unit) throws interruptedexception { long nanos = unit.tonanos(timeout); final reentrantlock lock = this.lock; lock.lockinterruptibly(); try { for (;;) { if (count != 0) { e x = extract(); return x; } if (nanos <= 0) return null; try { nanos = notempty.awaitnanos(nanos); } catch (interruptedexception ie) { notempty.signal(); // propagate to non-interrupted thread throw ie; } } } finally { lock.unlock(); } } //获取但不移除此队列的头;如果此队列为空,则返回 null。 public e peek() { final reentrantlock lock = this.lock; lock.lock(); try { return (count == 0) ? null : items[takeindex]; } finally { lock.unlock(); } } /** * 返回此队列中元素的数量。 */ public int size() { final reentrantlock lock = this.lock; lock.lock(); try { return count; } finally { lock.unlock(); } } /** *返回在无阻塞的理想情况下(不存在内存或资源约束)此队列能接受的其他元素数量。 */ public int remainingcapacity() { final reentrantlock lock = this.lock; lock.lock(); try { return items.length - count; } finally { lock.unlock(); } } /** * 从此队列中移除指定元素的单个实例(如果存在)。 */ public boolean remove(object o) { if (o == null) return false; final e[] items = this.items; final reentrantlock lock = this.lock; lock.lock(); try { int i = takeindex; int k = 0; for (;;) { if (k++ >= count) return false; if (o.equals(items[i])) { removeat(i); return true; } i = inc(i); } } finally { lock.unlock(); } } /** * 如果此队列包含指定的元素,则返回 true。 */ public boolean contains(object o) { if (o == null) return false; final e[] items = this.items; final reentrantlock lock = this.lock; lock.lock(); try { int i = takeindex; int k = 0; while (k++ < count) { if (o.equals(items[i])) return true; i = inc(i); } return false; } finally { lock.unlock(); } } …… }
更多java集合框架 arrayblockingqueue应用分析。
其它类似信息

推荐信息