java中linkedblockingqueue的增加方法

2026-01-29 0 71,436

在linkedblockingqueue的增加元素上,也我们之前所学的ArrayBlockingQueue有相通的地方。这里我们以其中的put方法进行举例,需要加入一些锁的使用对队列进行约束。下面就linkedblockingqueue增加元素的方法进行put的使用事项讲解,同时展示出对应的代码实例。

1.put方法使用事项

1)使用putLock加锁;

2)如果队列满了就阻塞在notFull条件上;

3)否则就入队;

4)如果入队后元素数量小于容量,唤醒其它阻塞在notFull条件上的线程;

5)释放锁;

6)如果放元素之前队列长度为0,就唤醒notEmpty条件;

2.put增加元素实例

 public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();   //e不能为null
        int c = -1;
        Node node = new Node(e);
        final ReentrantLock putLock = this.putLock;     //获取put锁
        final AtomicInteger count = this.count;          //获取count
        putLock.lockInterruptibly();
        try {
            while (count.get() == capacity) {        //如果满了,那么就需要使用notFull阻塞
                notFull.await();
            }
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)                    //如果此时又有空间了,那么notFull唤醒
                notFull.signal();
        } finally {
            putLock.unlock();             //释放锁
        }
        if (c == 0)            //当c为0时候,也要根take锁说一下,并发下
            signalNotEmpty();        //调用notEmpty        
    }
public E take() throws InterruptedException {
    E x;
    int c = -1;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    // 使用takeLock加锁
    takeLock.lockInterruptibly();
    try {
        // 如果队列无元素,则阻塞在notEmpty条件上
        while (count.get() == 0) {
            notEmpty.await();
        }
        // 否则,出队
        x = dequeue();
        // 获取出队前队列的长度
        c = count.getAndDecrement();
        // 如果取之前队列长度大于1,则唤醒notEmpty
        if (c > 1)
            notEmpty.signal();
    } finally {
        // 释放锁
        takeLock.unlock();
    }
    // 如果取之前队列长度等于容量
    // 则唤醒notFull
    if (c == capacity)
        signalNotFull();
    return x;
}
 
private E dequeue() {
    // head节点本身是不存储任何元素的
    // 这里把head删除,并把head下一个节点作为新的值
    // 并把其值置空,返回原来的值
    Node h = head;
    Node first = h.next;
    h.next = h; // help GC
    head = first;
    E x = first.item;
    first.item = null;
    return x;
}
 
private void signalNotFull() {
    final ReentrantLock putLock = this.putLock;
    putLock.lock();
    try {
        // 唤醒notFull
        notFull.signal();
    } finally {
        putLock.unlock();
    }
}

以上就是java中linkedblockingqueue增加元素的方法,相信经过之前ArrayBlockingQueue的学习,以及结合本篇put使用事项的了解,大家已经能够学会对于其入队的相关操作了。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 java教程 java中linkedblockingqueue的增加方法 https://www.zhiup.top/11040.html

相关