来源:http://blog.csdn.net/hudashi/article/details/7076745
ArrayBlockingQueue
它的容纳大小是固定的。此队列按 FIFO(先进先出)原则对元素进行排序。
队列的头部 是在队列中存在时间最长的元素。队列的尾部 是在队列中存在时间最短的元素。
新元素插入到队列的尾部,队列检索操作则是从队列头部开始获得元素。
这是一个典型的“有界缓存区”,固定大小的数组在其中保持生产者插入的元素和使用者提取的元素。
一旦创建了这样的缓存区,就不能再增加其容量。
试图向已满队列中放入元素会导致放入操作受阻塞,直到BlockingQueue里有新的空间才会被唤醒继续操作;
试图从空队列中检索元素将导致类似阻塞,直到BlocingkQueue进了新货才会被唤醒。
此类支持对等待的生产者线程和使用者线程进行排序的可选公平策略。
默认情况下,不保证是这种排序。然而,通过在构造函数将公平性 (fairness) 设置为 true 而构造的队列允许按照 FIFO 顺序访问线程。
公平性通常会降低吞吐量,但也减少了可变性和避免了“不平衡性”。
此类及其迭代器实现了 Collection 和 Iterator 接口的所有可选 方法。
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ArrayBlockingQueue(int capacity)
Creates an
ArrayBlockingQueue with the given (fixed) capacity and default access policy.
| |||||||||||
ArrayBlockingQueue(int capacity, boolean fair)
Creates an
ArrayBlockingQueue with the given (fixed) capacity and the specified access policy.
| |||||||||||
ArrayBlockingQueue(int capacity, boolean fair,
Collection<? extends E> c)
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.
|
Leak的对象是AbstractQueuedSynchronizer.Node,
据称JDK5会在Update12里Fix,JDK6会在Update2里Fix。
更加详细参考http://sesame.javaeye.com/blog/428026
以下是我自己写的生产者-使用者场景的一个用例
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
BlockingQueue queue=new ArrayBlockingQueue(100);
// TODO Auto-generated method stub
for(int i=0;i<10;i++)
new Thread(new ThreadProducer(queue)).start();
for(int i=0;i<10;i++)
new Thread(new ThreadConsumer(queue)).start();
}
}
class ThreadProducer implements Runnable
{
ThreadProducer(BlockingQueue queue)
{
this.queue=queue;
}
BlockingQueue queue;
static int cnt=0;
public void run()
{
String cmd;
while(true)
{
cmd=""+(cnt);
cnt=(cnt+1)&0xFFFFFFFF;
try{
queue.put(cmd);
Thread.sleep(1);
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class ThreadConsumer implements Runnable
{
ThreadConsumer(BlockingQueue queue)
{
this.queue=queue;
}
BlockingQueue queue;
public void run()
{
String cmd;
while(true)
{
try{
System.out.println(queue.take());
Thread.sleep(1);
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}