在java中使用双锁实现阻塞队列(一)

双锁实现1.0

使用单锁实现阻塞队列存在一个问题,因为offer和poll使用的是同一个锁,所以当执行offer操作时,poll就要等待offer释放锁之后才能进行操作,如果当poll正在执行时,offer也要等到poll解锁后才能进行。

解决方案:

创建两个锁对象,tailLock 和headLock分别关联offer方法和poll方法,分别创建与两个锁相关联的条件变量,让两个操作之间互不影响。

代码实现:

package com.luas.blockingqueue;
​
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
​
/**
 * @author by FZB
 * @date 2023/7/25
 * java阻塞队列双锁实现
 *
 * java单锁实阻塞队列存在一个问题,offer和poll共用一把锁,所以offer和poll无法同时进行
 * 解决方法就是创建两把锁
 */
public class BlockingQueue2<E> implements BlockingQueue<E> {
​
    public final E[] array;
    private int head;
    private int tail;
    private int size;
​
    /**
     * 条件变量用于实现线程的等待和唤醒操作。
     * 当一个线程需要等待某个条件满足时,它可以调用条件对象的 await() 方法进入等待状态,
     * 直到其他线程通过调用条件对象的 signal() 或 signalAll() 方法来唤醒它。在唤醒后,
     * 线程会重新尝试获取锁,然后继续执行。
     */
    private ReentrantLock tailLock = new ReentrantLock();
    private Condition tailWaits = tailLock.newCondition();
    private ReentrantLock headLock = new ReentrantLock();
    private Condition headWaits = headLock.newCondition();
​
​
​
    //此构造方法用来给给数组长度赋值
    public BlockingQueue2(int capacity) {
        this.array = (E[]) new Object[capacity];
    }
​
    /**
     * 判断数组是否为空
     *
     * @return
     */
    private boolean isEmpty() {
        return size == 0;
    }
​
    /**
     * 判断数组是否已满
     *
     * @return
     */
    private boolean isFull() {
        return size == array.length;
    }
​
​
    @Override
    public void offer(E e) throws InterruptedException {
        tailLock.lockInterruptibly();
        try{
            //判断是否为空
            while (isFull()){
                tailWaits.await();
            }
            array[tail] = e;
            if(++tail == array.length){
                tail=0;
            }
            size++;
​
        }finally {
            tailLock.unlock();
        }
​
    }
​
    @Override
    public boolean offer(E e, long timeout) throws InterruptedException {
        return false;
    }
​
    @Override
    public E poll() throws InterruptedException {
        headLock.lock();
        try{
            //1.判断队列是否为空
            if (isEmpty()) {
                headWaits.await();
            }
            //2.取出元素
            E e = array[head];
            array[head] = null;//help GC
            if(++head == array.length){
                head = 0;
            }
            //3.size--
            size--;
            return e;
​
        }finally {
            headLock.unlock();
        }
​
    }
}
​

双锁实现2.0

1.0版本还存在以下问题,size自增或者自减要执行三步操作,如果当offer和poll都执行操作时size++和size--的三个步骤可能会交替执行,那么就会出现错误,虽然size也受锁的保护,但是只会在使用tailLock锁的方法时被保护,例如执行offer操作时,另一个offer操作无法执行,无法改变size的值,但是如果执行的两个线程使用的是不同的锁,则是线程不安全的

解决方法:

原子变量

private AtomicInteger size = new AtomicInteger()

AtomicInteger 提供了一系列的原子操作,如增加、减少、获取和设置等,这些操作都是对整数变量进行操作的。与普通的 int 类型变量相比,AtomicInteger 可以在多线程环境下安全地进行增加和减少操作,而不需要使用显式的锁或同步机制。

以下是一些常用的 AtomicInteger 操作:

  • get():获取当前值

  • set(int newValue):设置新值

  • getAndSet(int newValue):获取当前值并设置新值

  • compareAndSet(int expect, int update):比较当前值是否等于期望值,如果是,则更新为新值

  • getAndIncrement():获取当前值并加一

  • getAndDecrement():获取当前值并减一

  • incrementAndGet():加一并获取新值

  • decrementAndGet():减一并获取新值

使用 AtomicInteger 可以避免使用显式的锁或同步机制,从而提高多线程程序的的可伸缩性和性能。它通常用于需要原子性操作的共享变量,例如计数器、标记等。

要创建 AtomicInteger 对象,可以使用其构造函数或者静态工厂方法。以下是创建 AtomicInteger 对象的示例代码:

使用构造函数创建:

java复制代码
​
AtomicInteger atomicInt = new AtomicInteger(initialValue);

在使用构造函数创建时,可以指定初始值作为参数。

使用静态工厂方法创建:

java复制代码
​
AtomicInteger atomicInt = AtomicInteger.allocate();
size++需要执行三步操作
1.读取成员变量的size值
2.自增
3.结果写回成员变量size
package com.luas.blockingqueue;
​
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
​
/**
 * @author by FZB
 * @date 2023/7/25
 * java阻塞队列双锁实现
 *
 * java单锁实阻塞队列存在一个问题,offer和poll共用一把锁,所以offer和poll无法同时进行
 * 解决方法就是创建两把锁
 */
public class BlockingQueue2<E> implements BlockingQueue<E> {
​
    public final E[] array;
    private int head;
    private int tail;
    private AtomicInteger size = new AtomicInteger();
​
    /**
     * 条件变量用于实现线程的等待和唤醒操作。
     * 当一个线程需要等待某个条件满足时,它可以调用条件对象的 await() 方法进入等待状态,
     * 直到其他线程通过调用条件对象的 signal() 或 signalAll() 方法来唤醒它。在唤醒后,
     * 线程会重新尝试获取锁,然后继续执行。
     */
    private ReentrantLock tailLock = new ReentrantLock();
    private Condition tailWaits = tailLock.newCondition();
    private ReentrantLock headLock = new ReentrantLock();
    private Condition headWaits = headLock.newCondition();
​
​
​
    //此构造方法用来给给数组长度赋值
    public BlockingQueue2(int capacity) {
        this.array = (E[]) new Object[capacity];
    }
​
    /**
     * 判断数组是否为空
     *
     * @return
     */
    private boolean isEmpty() {
        return size.get() == 0;
    }
​
    /**
     * 判断数组是否已满
     *
     * @return
     */
    private boolean isFull() {
        return size.get() == array.length;
    }
​
​
    @Override
    public void offer(E e) throws InterruptedException {
        tailLock.lockInterruptibly();
        try{
            //判断是否为空
            while (isFull()){
                tailWaits.await();
            }
            array[tail] = e;
            if(++tail == array.length){
                tail=0;
            }
            /**
             * size++需要执行三步操作
             * 1.读取成员变量的size值
             * 2.自增
             * 3.结果写回成员变量size
             */
            //获取整数值并自增,它的内部能够保证自增的原子性,不会与其他线程产生指令交错的问题
            size.getAndIncrement();
​
        }finally {
            tailLock.unlock();
        }
​
    }
​
    @Override
    public boolean offer(E e, long timeout) throws InterruptedException {
        return false;
    }
​
    @Override
    public E poll() throws InterruptedException {
        headLock.lock();
        try{
            //1.判断队列是否为空
            if (isEmpty()) {
                headWaits.await();
            }
            //2.取出元素
            E e = array[head];
            array[head] = null;//help GC
            if(++head == array.length){
                head = 0;
            }
            //3.size--
            size.getAndDecrement();
            return e;
​
        }finally {
            headLock.unlock();
        }
​
    }
}
​

双锁实现3.0

1.0和2.0版本没有添加唤醒线程

3.0对其进行了完善

在offer和poll方法中进行了相互唤醒的操作

注意:

条件变量使用时,必须要与相关联的锁一起使用,否则会报错

//4.唤醒offer线程
tailLock.lock();
try {
    tailWaits.signal();
} finally {
    tailLock.unlock();
}

但是3.0版本还有一个最严重的问题,会产生死锁

注意:锁一定不能嵌套使用,会产生死锁的问题

package com.luas.blockingqueue;
​
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
​
/**
 * @author by FZB
 * @date 2023/7/25
 * java阻塞队列双锁实现
 *
 * java单锁实阻塞队列存在一个问题,offer和poll共用一把锁,所以offer和poll无法同时进行
 * 解决方法就是创建两把锁
 */
public class BlockingQueue2<E> implements BlockingQueue<E> {
​
    public final E[] array;
    private int head;
    private int tail;
    private AtomicInteger size = new AtomicInteger();
​
    /**
     * 条件变量用于实现线程的等待和唤醒操作。
     * 当一个线程需要等待某个条件满足时,它可以调用条件对象的 await() 方法进入等待状态,
     * 直到其他线程通过调用条件对象的 signal() 或 signalAll() 方法来唤醒它。在唤醒后,
     * 线程会重新尝试获取锁,然后继续执行。
     */
    private ReentrantLock tailLock = new ReentrantLock();
    private Condition tailWaits = tailLock.newCondition();
    private ReentrantLock headLock = new ReentrantLock();
    private Condition headWaits = headLock.newCondition();
​
​
​
    //此构造方法用来给给数组长度赋值
    public BlockingQueue2(int capacity) {
        this.array = (E[]) new Object[capacity];
    }
​
    /**
     * 判断数组是否为空
     *
     * @return
     */
    private boolean isEmpty() {
        return size.get() == 0;
    }
​
    /**
     * 判断数组是否已满
     *
     * @return
     */
    private boolean isFull() {
        return size.get() == array.length;
    }
​
​
    @Override
    public void offer(E e) throws InterruptedException {
        tailLock.lockInterruptibly();
        try{
            //判断是否为空
            while (isFull()){
                //当offer线程发现是Full时进入tailWaits等待
                tailWaits.await();
            }
            array[tail] = e;
            if(++tail == array.length){
                tail=0;
            }
            /**
             * size++需要执行三步操作
             * 1.读取成员变量的size值
             * 2.自增
             * 3.结果写回成员变量size
             */
            //获取整数值并自增,它的内部能够保证自增的原子性,不会与其他线程产生指令交错的问题
            size.getAndIncrement();
​
            //4.唤醒等待非空的poll线程
            headLock.lock();
            try {
                headWaits.signal();
            } finally {
                headLock.unlock();
            }
​
        }finally {
            tailLock.unlock();
        }
​
    }
​
    @Override
    public boolean offer(E e, long timeout) throws InterruptedException {
        return false;
    }
​
    @Override
    public E poll() throws InterruptedException {
        headLock.lock();
        try{
            //1.判断队列是否为空
            if (isEmpty()) {
                headWaits.await();
            }
            //2.取出元素
            E e = array[head];
            array[head] = null;//help GC
            if(++head == array.length){
                head = 0;
            }
            //3.size--
            size.getAndDecrement();
​
            //4.唤醒offer线程
            tailLock.lock();
            try {
                tailWaits.signal();
            } finally {
                tailLock.unlock();
            }
​
            return e;
​
​
        }finally {
            headLock.unlock();
        }
​
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值