首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

有界缓存兑现基类

2013-09-05 
有界缓存实现基类public abstract class BaseBoundedBufferV{private final V[] bufprivate int tailp

有界缓存实现基类
public abstract class BaseBoundedBuffer<V>
{
    private final V[] buf;
   
    private int tail;
   
    private int head;
   
    private int count;
   
    @SuppressWarnings("unchecked")
    protected BaseBoundedBuffer(int capacity)
    {
        this.buf = (V[])new Object[capacity];
    }
   
    protected synchronized final void doPut(V v)
    {
        buf[tail] = v;
       
        if (++tail == buf.length)
        {
            tail = 0;
        }
       
        ++count;
    }
   
    protected synchronized final V doTake()
    {
        V v = buf[head];
       
        buf[head] = null;
       
        if (++head == buf.length)
        {
            head = 0;
        }
       
        --count;
       
        return v;
    }
   
    public synchronized final boolean isFull()
    {
        return count == buf.length;
    }
   
    public synchronized final boolean isEmpty()
    {
        return count == 0;
    }
}

热点排行