From:http://bradforj287.blogspot.com/2010/11/efficient-circular-buffer-in-java.html

 import java.util.NoSuchElementException;
/**
* Thread safe fixed size circular buffer implementation. Backed by an array.
*
* @author brad
*/
public class ArrayCircularBuffer<T> {
// internal data storage
private T[] data;
// indices for inserting and removing from queue
private int front = ;
private int insertLocation = ;
// number of elements in queue
private int size = ;
/**
* Creates a circular buffer with the specified size.
*
* @param bufferSize
* - the maximum size of the buffer
*/
public ArrayCircularBuffer(int bufferSize) {
data = (T[]) new Object[bufferSize];
}
/**
* Inserts an item at the end of the queue. If the queue is full, the oldest
* value will be removed and head of the queue will become the second oldest
* value.
*
* @param item
* - the item to be inserted
*/
public synchronized void insert(T item) {
data[insertLocation] = item;
insertLocation = (insertLocation + ) % data.length;
/**
* If the queue is full, this means we just overwrote the front of the
* queue. So increment the front location.
*/
if (size == data.length) {
front = (front + ) % data.length;
} else {
size++;
}
}
/**
* Returns the number of elements in the buffer
*
* @return int - the number of elements inside this buffer
*/
public synchronized int size() {
return size;
}
/**
* Returns the head element of the queue.
*
* @return T
*/
public synchronized T removeFront() {
if (size == ) {
throw new NoSuchElementException();
}
T retValue = data[front];
front = (front + ) % data.length;
size--;
return retValue;
}
/**
* Returns the head of the queue but does not remove it.
*
* @return
*/
public synchronized T peekFront() {
if (size == ) {
return null;
} else {
return data[front];
}
}
/**
* Returns the last element of the queue but does not remove it.
*
* @return T - the most recently added value
*/
public synchronized T peekLast() {
if (size == ) {
return null;
} else {
int lastElement = insertLocation - ;
if (lastElement < ) {
lastElement = data.length - ;
}
return data[lastElement];
}
}
}

最新文章

  1. BZOJ 1415 【NOI2005】 聪聪和可可
  2. 配置Linux任务计划
  3. 简单的Hibernate入门简介
  4. SQL_SERVER_2008升级SQL_SERVER_2008_R2办法 (一、升级;二、重新xie载安装)
  5. (转)PHP中extract()函数的妙用
  6. POJ 1797 Heavy Transportation 最短路变形(dijkstra算法)
  7. sudo su&ndash; user
  8. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来
  9. java_自定义标签运行原理
  10. Nginx(一)------简介与安装
  11. Canvas画圆形
  12. php 压缩文件 zip
  13. 1、Python中的正则表达式(0601)
  14. 牛客HJ浇花。
  15. 走楼梯(walk) 解题报告
  16. 《Population Based Training of Neural Networks》论文解读
  17. How to install SharePoint 2013 on Windows Server 2012 R2
  18. MVC中使用Hangfire执行定时任务
  19. 浏览器Request Header和Response Header的内容
  20. UVC调试

热门文章

  1. 用vue.js学习es6(一):基本工具及配置
  2. .Net Core Linux centos7行—发布程序到生产环境
  3. 【转】iOS,搜索标签布局
  4. C#委托与事件
  5. Sublime Text 配置代码
  6. 怎么把电脑的word,txt,pdf等文件拷贝到iPhone手机上
  7. react native 环境配置
  8. sift特征
  9. SQLServer------Join的使用方法
  10. 自制-随机生成不重复的数组 --算法,egret平台下的TS code