生产者消费者问题 传统版+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();
    }
}

发布了39 篇原创文章 · 获赞 19 · 访问量 1473

猜你喜欢

转载自blog.csdn.net/weixin_44222272/article/details/105335702