JUC学习之生产者消费者案例-Lock-Condition线程通信

本文是看视频学习笔记,若有错误请指正!

生产者消费者案例的lock写法

其中涉及到了Condition的用法,简答的介绍一下:

1.Condition接口描述了可能会与锁相关的条件变量,这些变量在用法上与使用Object.wait访问的隐式监视器类似,但提供了更强大的功能。需要特别之处的是,单个lock可能会与多个Condition对象关联,为了避免兼容性问题,Condition方法的名称与对应的Object版本中的不同。

2.在Condition对象中,与wait,notify,notifyAll方法对应的分别是await,signal,signalAll

3.Condition实例实质上被绑定到一个锁上。要为特定的Lock实例获得Condition,使用newCondition()方法即可。


具体代码:

public class TestProductorAndCustomerForLock {

    public static void main(String[] args){
        Clerk clerk = new Clerk();
        Productor productor = new Productor(clerk);
        Customer customer = new Customer(clerk);

        new Thread(productor,"productor A").start();
        new Thread(productor,"productor B").start();
        new Thread(customer,"customer A").start();
        new Thread(customer,"customer B").start();
    }
}

class Clerk {
    private int productor = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    //进货
    public  void get() {
        lock.lock();
        try {
            while (productor >= 1) {
                System.out.println("产品已满!");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + ":" + ++productor);
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }

    //卖货
    public  void sale() {
        lock.lock();
        try {
            while (productor <= 0) {
                System.out.println("缺货!");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + ":" + --productor);
            condition.signalAll();

        }finally {
            lock.unlock();
        }

    }
}


class Productor implements Runnable{

    private Clerk clerk;

    public Productor(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            clerk.get();
        }
    }
}

class Customer implements Runnable{
    private Clerk clerk;

    public Customer(Clerk clerk) {
        this.clerk = clerk;
    }

    @Override
    public void run() {
        for (int i = 0; i <20 ; i++) {
            clerk.sale();
        }
    }
}
运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值