生产者消费者问题 传统版+Lock版

题目 现在有生产者消费者两个线程 可以对初始值为0的一个变量进行操作

​ 实现生产者线程对该变量加1 消费者线程立刻对该变量减1

​ 实现交替 来10轮 (我们要求生产1个后 马上就进行消费)(消费一个后 就马上进行生产)

注意 生产者加1之后 消费者立马减1 结果应该是 1 0 1 0 1 0 …

传统版
package com.zyk;

//资源类
class ShareData{
    private int number=0;
    //生产者
    public synchronized void increment() throws InterruptedException {
        //1  判断
        while(number!=0){
            this.wait();
        }
        //2  干活
        number++;
        System.out.println(Thread.currentThread().getName()+"\t"+number);
        //3  通知
        this.notify();
    }
    //消费者
    public synchronized void decrement() throws InterruptedException {
        //1  判断
        while(number==0){
            this.wait();
        }
        //2  干活
        number--;
        System.out.println(Thread.currentThread().getName()+"\t"+number);
        //3  通知
        this.notify();
    }
}
public class Product_ConsumerDemo1 {
    public static void main(String[] args) {
        ShareData shareData=new ShareData();
        for (int i = 1; i <=10 ; i++) {
            new Thread(()->{
                try {
                    shareData.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"生产者").start();
        }

        for (int i = 1; i <=10 ; i++) {
            new Thread(()->{
                try {
                    shareData.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"消费者").start();
        }
    }
}
Lock版
package com.zyk;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class MyShare{
    private int num=0;

    private Lock lock=new ReentrantLock();

    private Condition condition=lock.newCondition();

    public void increment()throws Exception{
        lock.lock();
        try {
            //判断
            while (num!=0){
                condition.await();
            }
            //干活
            num++;
            System.out.println(Thread.currentThread().getName()+"\t 生产了"+num);
            //通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decrement()throws  Exception{
        lock.lock();
        try {
            //判断
            while (num==0){
                condition.await();
            }
            //干活
            num--;
            System.out.println(Thread.currentThread().getName()+"\t 消费了"+num);
            //通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
public class Product_ConsumerDemo2 {

    public static void main(String[] args) {
        MyShare myShare=new MyShare();
        new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                try {
                    myShare.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"AAA").start();

        new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                try {
                    myShare.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"BBB").start();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值