当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现简易生产者消费者模型过程解析

Java实现简易生产者消费者模型过程解析

2020年06月23日  | 移动技术网IT编程  | 我要评论

一、概述

一共两个线程,一个线程生产产品,一个线程消费产品,使用同步代码块方法,同步两个线程。当产品没有时,通知生产者生产,生产者生产后,通知消费者消费,并等待消费者消费完。

需要注意的是,有可能出现,停止生产产品后,消费者还没未来得及消费生产者生产的最后一个产品,就结束消费,导致最后一个产品没有被消费。

本例使用synchronize以及wait()、notify()实现简易版的线程者消费者模型。

二、测试用例

这里的产品用笔来演示,每只笔都有其编号code

一共有四个类:分别是生产者类,产品类,消费者类,测试类

产品

package test.exception.producer_consumer_model;

/*
假设为产品为笔
 */

public class production {
  private string type = "";
  private string color = "";
  private long code = 0; // 产品编号
  private boolean isproduced = false; // 是否生产完成 初始状态为未生产状态
  private boolean iscontinueproduce = true; // 是否停产该产品

  public void setcontinueproduce(boolean continueproduce) {
    iscontinueproduce = continueproduce;
  }

  public void setcode(long code) {
    this.code = code;
  }

  public production(){
  }

  public boolean iscontinueproduce() {
    return iscontinueproduce;
  }

  public void settype(string type) {
    this.type = type;
  }

  public void setcolor(string color) {
    this.color = color;
  }

  public void setproduced(boolean produced) {
    isproduced = produced;
  }

  public boolean isproduced() {
    return isproduced;
  }

  @override
  public string tostring() {
    return color + type + "-" + code;
  }
}

生产者

package test.exception.producer_consumer_model;

public class producer implements runnable {
  private final production pen; // 产品

  public producer(production pen) {
    this.pen = pen;
  }

  // 生产
  public void produce() {
    long code = 0;
    while (this.pen.iscontinueproduce()) {
      synchronized (this.pen) {
        if (this.pen.isproduced()) {
          try {
            this.pen.wait(); // 等待消费者消费
          } catch (interruptedexception e) {
            e.printstacktrace();
          }
        }
        // 开始生产
        this.pen.settype("铅笔");
        this.pen.setcolor("蓝色");
        this.pen.setcode(code++);
        this.pen.setproduced(true);
        system.out.println(this.pen + " is produced");
        this.pen.notify();
      }
    }
    system.out.println("finish producing");
  }
  @override
  public void run() {
    produce();
  }
}

消费者

package test.exception.producer_consumer_model;

public class consumer implements runnable {
  private final production pen;

  public consumer(production pen) {
    this.pen = pen;
  }

  // 持续消费
  public void consumer() {
    while (this.pen.iscontinueproduce()) {
      synchronized (this.pen) {
        if (!this.pen.isproduced()) {
          try {
            this.pen.wait(); // 等待生产者生产
          } catch (interruptedexception e) {
            e.printstacktrace();
          }
        }

        system.out.println(this.pen + " is consumed"); // 使用
        this.pen.setproduced(false); // 使用完后更新状态
        this.pen.notify();
      }
    }
    // 确保停止生产后,能够使用最后生产的一支笔
    if (this.pen.isproduced()) {
      system.out.println(this.pen + " is consumed");
    }

    system.out.println("finish using");
  }

  @override
  public void run() {
    consumer();
  }
}

主线程测试

package test.exception.producer_consumer_model;

public class demo {
  public static void main(string[] args) throws interruptedexception {
    production pen = new production();
    consumer consumer = new consumer(pen);
    producer producer = new producer(pen);
    new thread(producer).start(); // 开启生产者线程
    new thread(consumer).start(); // 开启消费者线程

    thread.sleep(10000);
    pen.setcontinueproduce(false); // 10s后停止生产该类型的笔
  }
}

运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网