0
点赞
收藏
分享

微信扫一扫

学习博客:初始线程协作

祈澈菇凉 2022-03-24 阅读 33

针对生产者消费者问题

这是一个线程同步问题,生产者和消费者共享同一个产品,并且生产者和消费者之间相互依赖,互为条件

在此问题中,仅使用synchronized是不够的

两种解决办法:管程法、信号灯法

管程法

//测试 生产者消费者模型-->利用缓冲区解决 管程法

//生产者 消费者 产品 缓冲区
public class TestPC {
    public static void main(String[] args) {
        SynContainer container = new SynContainer();

        new Productor(container).start();
        new Consumer(container).start();

    }
}

//生产者
class Productor extends Thread{
    SynContainer container;

    public Productor(SynContainer container){
        this.container = container;
    }

    //生产
    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            container.push(new Bread(i));
            System.out.println("生产第" + i + "个面包");
        }
    }
}

//消费者
class Consumer extends Thread{
    SynContainer container;

    public Consumer(SynContainer container){
        this.container = container;
    }

    //消费
    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            System.out.println("消费第" + container.pop().id + "个面包");
        }
    }
}

//产品
class Bread{
    int id; //产品编号

    public Bread(int id) {
        this.id = id;
    }
}

//缓冲区
class SynContainer{
    //容器大小
    Bread[] breads = new Bread[10];

    //容器计数器
    int count = 0;

    //生产者放入产品
    public synchronized void push(Bread bread){
        //如果容器满了,需要等待消费者消费面包
        if(count == breads.length){
            //通知消费者消费,生产等待
            try {
                this.wait();    //等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        //如果没有满,需要生产者生产面包
        breads[count] = bread;
        count++;

        //通知消费者消费
        this.notifyAll();

    }

    //消费者消费产品
    public synchronized Bread pop(){
        //判断能否消费
        if(count == 0){
            //等待生产者生产,消费者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //如果可以消费
        count--;
        Bread bread = breads[count];

        //通知生产者生产
        this.notifyAll();

        return bread;
    }
}

在这里插入图片描述
信号灯法

//测试生产者消费者问题 信号灯法 设置标志位
public class TestPC2 {
    public static void main(String[] args) {
        Book book = new Book();
        new Writer(book).start();
        new Reader(book).start();

    }
}

//生产者  作家
class Writer extends Thread{
    Book book;
    public Writer(Book book){
        this.book = book;
    }

    //出版
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if(i%2==0){
                this.book.publish("深入理解Java虚拟机");
            }else{
                this.book.publish("剑指offer");
            }
        }
    }
}

//消费者  读者
class Reader extends Thread{
    Book book;
    public Reader(Book book){
        this.book = book;
    }

    //阅读
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            book.read();
        }
    }
}

//产品 书
class Book{
    //作家写书,读者等待
    //读者观看,作家等待
    String book;
    boolean flag = true;

    //出版
    public synchronized void publish(String book){

        if(!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("作家出版了:" + book);
        //通知读者阅读
        this.notifyAll();
        this.book = book;
        this.flag = !this.flag; //切换标志位
    }

    //阅读
    public synchronized void read(){
        if(flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("阅读了:" + book);

        //通知作家出版
        this.notifyAll();
        this.flag = !this.flag; //切换标志位
    }
}

在这里插入图片描述

举报

相关推荐

0 条评论