Java 使用Lock、Condition的生产者与消费者问题(JUC)

synchronized 与 Lock 的区别:

  1. synchronized是内置的Java关键字;Lock是一个Java类。
  2. synchronized无法判断锁的状态;Lock可以判断是否获取到了锁。
  3. synchronized会自动释放锁;Lock必须手动释放锁,如果不释放,会造成死锁!
  4. synchronized线程1(获得锁,阻塞)、线程2(等待,傻傻的等);Lock不一定会一直等待下去(trylock)。
  5. synchronized是可重入锁、非公平、不可以中断的;Lock可重入锁、非公平(可以通过标志位设置)。
  6. synchronized适合锁少量的代码同步问题;Lock适合锁大量的代码同步问题。
    在这里插入图片描述

说到 Lock 就不得不说 Condition ,官方文档中关于Condition的解释:
在这里插入图片描述
Lock -> synchronized
Condition.await() -> Object.wait()
Condition.signalAll() -> Object.notifyAll();


代码实现:

通过Condition进行精准的通知和唤醒:

// 资源类    判断等待 -> 业务 -> 通知
class Data{
    private int number = 1;
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();

    public void printA() throws InterruptedException {
        lock.lock();
        try {
            // 业务代码
            while (number != 1) {
                // 等待
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName() + " -> " + number);
            number = 2;
            // 通知其他线程
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printB() throws InterruptedException {
        lock.lock();
        try {
            while (number != 2) {
                // 等待
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName() + " -> " + number);
            number = 3;
            // 通知其他线程
            condition3.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printC() throws InterruptedException {
        lock.lock();
        try {
            while(number != 3){
                // 等待
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName() + " -> " + number);
            number = 1;
            // 通知其他线程
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
// main方法
public class A {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
    }
}

结果如下:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值