您的位置:首页 > 产品设计 > UI/UE

ArrayBlockingQueue学习笔记

2015-09-15 20:30 127 查看
官网说明:

一个由数组支持的有界阻塞队列。此队列按 FIFO(先进先出)原则对元素进行排序。队列的头部 是在队列中存在时间最长的元素。队列的尾部 是在队列中存在时间最短的元素。新元素插入到队列的尾部,队列获取操作则是从队列头部开始获得元素。

这是一个典型的“有界缓存区”,固定大小的数组在其中保持生产者插入的元素和使用者提取的元素。一旦创建了这样的缓存区,就不能再增加其容量。试图向已满队列中放入元素会导致操作受阻塞;试图从空队列中提取元素将导致类似阻塞。

此类支持对等待的生产者线程和使用者线程进行排序的可选公平策略。默认情况下,不保证是这种排序。然而,通过将公平性 (fairness) 设置为 true 而构造的队列允许按照 FIFO 顺序访问线程。公平性通常会降低吞吐量,但也减少了可变性和避免了“不平衡性”。

主要方法:

boolean	add(E e) 
          将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量),在成功时返回 true,如果此队列已满,则抛出 IllegalStateException。
 void	clear() 
          自动移除此队列中的所有元素。
 boolean	contains(Object o) 
          如果此队列包含指定的元素,则返回 true。
 int	drainTo(Collection<? super E> c) 
          移除此队列中所有可用的元素,并将它们添加到给定 collection 中。
 int	drainTo(Collection<? super E> c, int maxElements) 
          最多从此队列中移除给定数量的可用元素,并将这些元素添加到给定 collection 中。
 Iterator<E>	iterator() 
          返回在此队列中的元素上按适当顺序进行迭代的迭代器。
 boolean	offer(E e) 
          将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量),在成功时返回 true,如果此队列已满,则返回 false。
 boolean	offer(E e, long timeout, TimeUnit unit) 
          将指定的元素插入此队列的尾部,如果该队列已满,则在到达指定的等待时间之前等待可用的空间。
 E	peek() 
          获取但不移除此队列的头;如果此队列为空,则返回 null。
 E	poll() 
          获取并移除此队列的头,如果此队列为空,则返回 null。
 E	poll(long timeout, TimeUnit unit) 
          获取并移除此队列的头部,在指定的等待时间前等待可用的元素(如果有必要)。
 void	put(E e) 
          将指定的元素插入此队列的尾部,如果该队列已满,则等待可用的空间。
 int	remainingCapacity() 
          返回在无阻塞的理想情况下(不存在内存或资源约束)此队列能接受的其他元素数量。
 boolean	remove(Object o) 
          从此队列中移除指定元素的单个实例(如果存在)。
 int	size() 
          返回此队列中元素的数量。
 E	take() 
          获取并移除此队列的头部,在元素变得可用之前一直等待(如果有必要)。
 Object[]	toArray() 
          返回一个按适当顺序包含此队列中所有元素的数组。
<T> T[]
toArray(T[] a) 
          返回一个按适当顺序包含此队列中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。


代码实例:

package com.bennytian.test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author BennyTian
 * @date 2015年8月28日 上午11:11:20
 */
public class TestArrayBlockingQueue {

	public static void main(String[] args) throws Exception {
		
		ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1000);
		
		ExecutorService service = Executors.newCachedThreadPool();
		
		service.execute(new ProducerWorker(queue));
		service.execute(new CustomerWorker(1,queue));
		service.execute(new CustomerWorker(2,queue));
		
//		service.shutdown();
		
	}
	
}

/**
 * 生产者
 * @author BennyTian
 * @date 2015年9月15日 下午8:05:58
 */
class ProducerWorker implements Runnable {
	
	private ArrayBlockingQueue<Integer> queue; 
	
	public ProducerWorker(ArrayBlockingQueue<Integer> queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		try {
			int i = 0;
			while (true && i < 100000) {
				System.out.println("put " + ++i);
				queue.put(i);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
	
}

/**
 * 消费者
 * @author BennyTian
 * @date 2015年9月15日 下午8:05:50
 */
class CustomerWorker implements Runnable{

	private ArrayBlockingQueue<Integer> queue; 
	private int id;
	
	public CustomerWorker(int id,ArrayBlockingQueue<Integer> queue) {
		this.id = id;
		this.queue = queue;
	}
	
	@Override
	public void run() {
		try{
			while(true){
				//没有可消费的信息之后 会一直租塞
				System.out.println(id + " take " + queue.take());
			}
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		
	}
	
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: