baseboundedbuffer.java

来自「java concurrency in practice 源码. JAVA」· Java 代码 · 共 47 行

JAVA
47
字号
package net.jcip.examples;import net.jcip.annotations.*;/** * BaseBoundedBuffer * <p/> * Base class for bounded buffer implementations * * @author Brian Goetz and Tim Peierls */@ThreadSafepublic abstract class BaseBoundedBuffer <V> {    @GuardedBy("this") private final V[] buf;    @GuardedBy("this") private int tail;    @GuardedBy("this") private int head;    @GuardedBy("this") private int count;    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;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?