【数据结构】基于JDK的ReentrantLock实现BlockingQueue

基于JDK的ReentrantLock实现BlockingQueue

BlockingQueue定义接口

/**
 * @author SurfingMaster
 * @date 2023/8/18
 **/
public interface BlockingQueue<E> {

    void offer(E e) throws InterruptedException;

    boolean offer(E e, long milliSecs) throws InterruptedException;

    E poll() throws InterruptedException;

}

接口实现


注意事项

  • 避免死锁
/**
 * TODO 阻塞、扩容
 * @author SurfingMaster
 * @date 2023/8/18
 **/
public class BlockingQueue03<E> implements BlockingQueue<E> {

    private final E[] arr;

    private int head = 0;
    private int tail = 0;

    private final AtomicInteger size = new AtomicInteger(0);


    private final ReentrantLock tailLock = new ReentrantLock(); // offerLock
    private final Condition tailWaits = tailLock.newCondition();

    private final ReentrantLock headLock = new ReentrantLock(); // pollLock
    private final Condition headWaits = tailLock.newCondition();


    @SuppressWarnings("all")
    public BlockingQueue03(int capacity) {
        this.arr = (E[]) new Object[capacity];
    }


    @Override
    public void offer(E e) throws InterruptedException {
        tailLock.lockInterruptibly();
        int cnt;
        try {
            while (isFull()) {
                tailWaits.await();
            }
            arr[tail] = e;
            if (++tail == arr.length) {
                tail = 0;
            }
            cnt = size.getAndIncrement();

            if (cnt < arr.length - 1) {
                tailWaits.signal();
            }
        } finally {
            tailLock.unlock();
        }

        //  避免锁中锁
        if (cnt == 0) {
            headLock.lock();
            try {
                headWaits.signal(); 
            } finally {
                headLock.unlock();
            }
        }
    }

    @Override
    public boolean offer(E e, long milliSecs) throws InterruptedException {
        tailLock.lockInterruptibly();
        int cnt;
        try {
            long remainTime = TimeUnit.DAYS.toNanos(milliSecs);
            while (isFull()) {
                if (remainTime <= 0) return false;
                remainTime = tailWaits.awaitNanos(remainTime);
            }
            arr[tail] = e;
            if (++tail == arr.length) {
                tail = 0;
            }
            cnt = size.getAndIncrement();

            if (cnt < arr.length - 1) { 
                tailWaits.signal();
            }
        } finally {
            tailLock.unlock();
        }

        // 避免锁中锁
        if (cnt == 0) {
            headLock.lock();
            try {
                headWaits.signal();
            } finally {
                headLock.unlock();
            }
        }
        return true;
    }

    @Override
    public E poll() throws InterruptedException {
        E res;
        int cnt;
        headLock.lockInterruptibly();
        try {
            while (isEmpty()) {
                headWaits.await();
            }
            res = arr[head];
            arr[head] = null;
            if (++head == arr.length) {
                head = 0;
            }
            cnt = size.getAndDecrement(); 

            if (cnt > 1) {
                headWaits.signal();
            }
        } finally {
            headLock.unlock();
        }

        //  避免锁中锁
        if (cnt == arr.length) {
            try {
                tailLock.lock();
                tailWaits.signal(); 
            } finally {
                tailLock.unlock();
            }
        }
        return res;
    }

    public boolean isEmpty() {
        return size.get() == 0;
    }

    public boolean isFull() {
        return size.get() == arr.length;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值