All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wowtools.common.pcm.BufferPool Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
package org.wowtools.common.pcm;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedTransferQueue;


/**
 * 生产者消费者队列的缓冲池,认为生产者产生的数据是无限的
 * 
 * @author liuyu
 * @param 
 */
public class BufferPool {
	protected final BlockingQueue queue;

	/**
	 * 构造一个指定容量的缓冲池,试图向已满队列中放入元素会导致操作受阻塞
	 * 
	 * @param capacity
	 *            缓冲池容量
	 */
	public BufferPool(int capacity) {
		queue = new ArrayBlockingQueue(capacity);
	}

	/**
	 * 构造一个无限容量的缓冲池
	 * 
	 * @param capacity
	 */
	public BufferPool() {
		queue = new LinkedTransferQueue();
	}

	/**
	 * 放入数据
	 * 
	 * @param obj
	 */
	public void add(T obj) {
		try {
			queue.put(obj);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 取出数据
	 * 
	 * @return
	 */
	public T take() {
		try {
			return queue.take();
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy