ArrayBlockingQueue<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">一个由数组支持的有界阻塞队列。它的本质是一个基于数组的blockingqueue的实现。<br style="line-height:25px"> 它的容纳大小是固定的。此队列按FIFO(先进先出)原则对元素进行排序。<br style="line-height:25px"> 队列的头部是在队列中存在时间最长的元素。队列的尾部是在队列中存在时间最短的元素。<br style="line-height:25px"> 新元素插入到队列的尾部,队列检索操作则是从队列头部开始获得元素。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">这是一个典型的“有界缓存区”,固定大小的数组在其中保持生产者插入的元素和使用者提取的元素。<br style="line-height:25px"> 一旦创建了这样的缓存区,就不能再增加其容量。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">试图向已满队列中放入元素会导致放入操作受阻塞,直到</span><span style="color:#993300; line-height:25px">BlockingQueue</span><span style="color:#000080; line-height:25px">里有新的空间才会被唤醒继续操作;<br style="line-height:25px"> 试图从空队列中检索元素将导致类似阻塞,直到</span><span style="color:#993300; line-height:25px">BlocingkQueue</span><span style="color:#000080; line-height:25px">进了新货才会被唤醒。</span><br style="line-height:25px"><span style="line-height:25px"></span><span style="color:#003366; line-height:25px">此类支持对等待的生产者线程和使用者线程进行排序的</span><span style="color:#993300; line-height:25px">可选公平策略</span><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">默认情况下,不保证是这种排序。然而,通过在构造函数将公平性(fairness)设置为true而构造的队列允许按照FIFO顺序访问线程。<br style="line-height:25px"> 公平性通常会降低吞吐量,但也减少了可变性和避免了“不平衡性”。</span><br style="line-height:25px"><span style="line-height:25px">此类及其迭代器实现了Collection和Iterator接口的所有可选方法。</span><br style="line-height:25px"><span style="line-height:25px"></span><span style="line-height:25px"><wbr style="line-height:25px">注意1</wbr></span><wbr style="line-height:25px"><span style="line-height:25px">:它是有界阻塞队列。它是数组实现的,是一个典型的“有界缓存区”。数组大小在构造函数指定,而且从此以后不可改变。</span><br style="line-height:25px"><span style="line-height:25px"></span><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px"><span style="line-height:25px">:是它线程安全的,是阻塞的,具体参考BlockingQueue的“注意4”。</span><br style="line-height:25px"><span style="line-height:25px"></span><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px"><span style="line-height:25px">:不接受null元素</span><br style="line-height:25px"><span style="line-height:25px"></span><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px"><span style="line-height:25px">:公平性(fairness)可以在构造函数中指定。</span><br style="line-height:25px"></wbr></wbr></wbr></wbr></wbr>
如果为true,则按照FIFO顺序访问插入或移除时受阻塞线程的队列;如果为false,则访问顺序是不确定的。
<wbr style="line-height:25px">注意5</wbr><wbr style="line-height:25px"><span style="line-height:25px">:它实现了BlockingQueue接口。关于BlockingQueue,请参照《</span><span style="line-height:24px; font-weight:bold; white-space:nowrap"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107415915820/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">BlockingQueue</a></span></wbr> 》
<wbr style="line-height:25px">注意6</wbr><wbr style="line-height:25px">:此类及其迭代器实现了Collection和Iterator接口的所有可选方法。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意7:</wbr></span><span style="line-height:25px">其容量在构造函数中指定。容量不可以自动扩展,也没提供手动扩展的接口。</span></wbr>
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ArrayBlockingQueue.html#ArrayBlockingQueue(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ArrayBlockingQueue</a></span>(int capacity)</nobr>
Creates an
ArrayBlockingQueue with the given (fixed) capacity and default access policy.
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ArrayBlockingQueue.html#ArrayBlockingQueue(int,%20boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ArrayBlockingQueue</a></span>(int capacity, boolean fair)</nobr>
Creates an
ArrayBlockingQueue with the given (fixed) capacity and the specified access policy.
| ||||||||||
<nobr style="line-height:21px"></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ArrayBlockingQueue.html#ArrayBlockingQueue(int,%20boolean,%20java.util.Collection<?%20extends%20E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">ArrayBlockingQueue</a></span>(int capacity, boolean fair,<a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Collection</a><?extendsE> c)</nobr>
Creates an
ArrayBlockingQueue with the given (fixed) capacity, the specified access policy and initially containing the elements of the given collection, added in traversal order of the collection's iterator.
|
<wbr style="line-height:25px">注意5</wbr><wbr style="line-height:25px"><span style="line-height:25px">:它实现了BlockingQueue接口。关于BlockingQueue,请参照《</span><span style="line-height:24px; font-weight:bold; white-space:nowrap"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107415915820/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">BlockingQueue</a></span></wbr> 》
<wbr style="line-height:25px">注意6</wbr><wbr style="line-height:25px">:此类及其迭代器实现了Collection和Iterator接口的所有可选方法。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意7:</wbr></span><span style="line-height:25px">其容量在构造函数中指定。容量不可以自动扩展,也没提供手动扩展的接口。</span></wbr>
注意8:在JDK5/6中,LinkedBlockingQueue和ArrayBlocingQueue等对象的poll(longtimeout,TimeUnitunit)存在内存泄露
Leak的对象是AbstractQueuedSynchronizer.Node,
据称JDK5会在Update12里Fix,JDK6会在Update2里Fix。
更加详细参考http://sesame.javaeye.com/blog/428026<wbr style="line-height:25px">和<a target="_blank" rel="nofollow" href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2143840" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2143840</a><wbr style="line-height:25px"></wbr></wbr>
<wbr style="line-height:25px"><div style="line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">实例1:</span><br style="line-height:25px"> 以下是我自己写的生产者-使用者场景的一个用例</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.ArrayBlockingQueue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.BlockingQueue;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">Test{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *@paramargs<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#ff00ff; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BlockingQueuequeue=newArrayBlockingQueue(100);</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//TODOAuto-generatedmethodstub</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<10;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadProducer(queue)).start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<10;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadConsumer(queue)).start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">ThreadProducerimplementsRunnable</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadProducer(BlockingQueuequeue)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.queue=queue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BlockingQueuequeue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticint</span><span style="color:#3366ff; line-height:25px">cnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringcmd;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(true)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cmd=""+(cnt);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cnt=(cnt+1)&0xFFFFFFFF;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">queue.put(cmd);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">e.printStackTrace();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">ThreadConsumerimplementsRunnable</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadConsumer(BlockingQueuequeue)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.queue=queue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BlockingQueuequeue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringcmd;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(true)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(queue.take());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">e.printStackTrace();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span></span></div> <div><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="color:#3366ff; line-height:25px"><br></span></span></div> </wbr>
Leak的对象是AbstractQueuedSynchronizer.Node,
据称JDK5会在Update12里Fix,JDK6会在Update2里Fix。
更加详细参考http://sesame.javaeye.com/blog/428026<wbr style="line-height:25px">和<a target="_blank" rel="nofollow" href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2143840" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2143840</a><wbr style="line-height:25px"></wbr></wbr>
<wbr style="line-height:25px"><div style="line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">实例1:</span><br style="line-height:25px"> 以下是我自己写的生产者-使用者场景的一个用例</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.ArrayBlockingQueue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.concurrent.BlockingQueue;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">Test{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *@paramargs<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#ff00ff; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">(String[]args){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BlockingQueuequeue=newArrayBlockingQueue(100);</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//TODOAuto-generatedmethodstub</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<10;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadProducer(queue)).start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<10;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">newThread(newThreadConsumer(queue)).start();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">ThreadProducerimplementsRunnable</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadProducer(BlockingQueuequeue)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.queue=queue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BlockingQueuequeue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">staticint</span><span style="color:#3366ff; line-height:25px">cnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringcmd;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(true)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cmd=""+(cnt);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cnt=(cnt+1)&0xFFFFFFFF;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">queue.put(cmd);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">e.printStackTrace();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">ThreadConsumerimplementsRunnable</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ThreadConsumer(BlockingQueuequeue)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.queue=queue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">BlockingQueuequeue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">run()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringcmd;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">(true)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(queue.take());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Thread.sleep(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(InterruptedExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">e.printStackTrace();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span></span></div> <div><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="color:#3366ff; line-height:25px"><br></span></span></div> </wbr>