
课程咨询: 400-996-5531 / 投诉建议: 400-111-8989
认真做教育 专心促就业
昆明IT培训的老师今天给大家讲有界缓存的基类。
package cn.xf.cp.ch14;
/**
*
*功能:有界缓存实现基类
*时间:下午2:20:00
*文件:BaseBoundedBuffer.java
*@author Administrator
*
* @param <V>
*/
public class BaseBoundedBuffer<V>
{
private final V[] buf;
private int tail;
private int head;
private int count;
public BaseBoundedBuffer(int capacity)
{
//初始化数组
this.buf = (V[]) new Object[capacity];
}
//放入一个数据,final方法无法被重写
protected synchronized final void doPut(V v)
{
buf[tail] = v;
if(++tail == buf.length)
{
tail = 0;
}
//插入一个方法,总量++
++count;
}
/**
*取出一个数据
* @return
*/
protected synchronized final V doTake()
{
V v = buf[head];
buf[head] = null;
if(++head == buf.length)
{
head = 0;
}
--count;
return v;
}
//通过对count的判断,来确定数组是否是满的
public synchronized final boolean isFull()
{
return count == buf.length;
}
public synchronized final boolean isEmpty()
{
return count == 0;
}
}