zoukankan      html  css  js  c++  java
  • java 生产者 与 消费者的案例

    主要理解了两个问题

       1.线程数据同步的问题

       2.线程交替运行的方式

    package ThreadDemo;

    /**
    * 生产者与消费者的案例(一,同步的问题,值的问题 二,交替执行的问题)
    * @author lile
    * 同步的问题(synchronized 知识点)
    * 交替执行的问题(notify ,wait, 线程等待)
    */
    public class ThreadDemo {
    public static void main(String[] args) {
    Food food = new Food();
    //生产者
    Producter p = new Producter(food);

    Consumer c = new Consumer(food);
    new Thread(p).start();
    new Thread(c).start();

    }
    }

    /**
    * 生产者的类
    * @author lile
    *
    */
    class Producter implements Runnable{
    private Food food;
    public Producter(Food food){
    this.food = food;
    }
    @Override
    public void run() {
    // TODO Auto-generated method stub


    for (int i = 0; i < 100; i++) {
    if(i%2==0){
    food.set("韭菜炒鸡蛋", "韭菜炒蛋");
    }else{
    food.set("腰花", "腰花");
    }
    }
    }


    }

    /**
    * 消费这的对象
    * @author lile
    *
    */
    class Consumer implements Runnable{

    private Food food;
    public Consumer(Food food){
    this.food = food;
    }

    @Override
    public void run() {
    // TODO Auto-generated method stub
    for (int i = 0; i <100; i++) {
    food.get();
    }

    }

    }

    /**
    * 产品对象
    * @author lile
    *
    */
    class Food{
    private Boolean flag = true; //true表示可以生产
    private String name;
    private String content;
    public Food(){

    }
    public Food(String name,String content){
    this.name = name;
    this.content = content;
    }

    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }


    //制作产品
    public synchronized void set(String name,String content){
    //两个线程交替运行
    if(!flag){
    //当前线程等待,没有指定时间,需要其他线程唤醒,释放对象所,让出cpu,不能生产
    try {
    this.wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    this.setName(name);
    try {
    Thread.sleep(300);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    this.setContent(content);
    this.flag = false; //表示可以消费
    this.notify(); //在监视器唤醒该线程
    }

    //消费
    public synchronized void get(){
    //两个线程交替运行
    if(flag){
    //当前线程等待,没有指定时间,需要其他线程唤醒,释放对象所,让出cpu,不能生产
    try {
    this.wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    try {
    Thread.sleep(300);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    this.flag = true; //表示可以消费
    this.notify(); //在监视器唤醒该线程
    System.out.println(this.getName()+this.getContent());
    }

    }








  • 相关阅读:
    2021.1.28 个人rating赛补题报告
    2021.1.23 个人rating赛补题报告
    2021.1.23 个人rating赛补题报告
    2020.12.14 个人训练赛补题报告
    2020.11.28 2020团体程序设计天梯赛补题报告
    2020.12.3 Codeforces Beta Round #73(Div2)补题报告
    Xhorse VVDI Prog V5.0.6 is Ready for BCM2 Adapter
    Program 2021 Ford Bronco All Keys Lost using VVDI Key Tool Plus
    Xhorse VVDI Prog V5.0.4 Software Update in July 2021
    How to use Xhorse VVDI2 to Exchange BMW FEM/BDC Module?
  • 原文地址:https://www.cnblogs.com/lilefordream/p/3700742.html
Copyright © 2011-2022 走看看