zoukankan      html  css  js  c++  java
  • JAVA多线程之生产者 消费者模式 妈妈做面包案例

    创建四个类
    1.面包类 锅里只可以放10个面包 ---装面包的容器
    2.厨房 kitchen 生产面包 和消费面包  最多生产100个面包
    3.生产者
    4消费者
    5.测试类

    多线程经典案例

    import java.util.Stack;

    //生产者消费者 问题 面包类
    public class Brake {
        //做面包的锅 最多十个面包
        public static final int MAX_NUMBER=10;
        Stack<Integer> stack=new Stack<Integer>();
        public static int BRAKE_NUM=0; //面包的个数
        
        //做面包
        public synchronized  void insert(int number)
        {
            stack.push(number);
            Brake.BRAKE_NUM++;//锅里面包数+1
        }
        
        //拿面包
        public synchronized int remove()
        {
            
            Brake.BRAKE_NUM--;
            return stack.pop(); //出栈
        }
    }
    -------------------------------------------------------------

    厨房类


    public class Kitchen {
        private Brake brake;
        public  static int bigNumber = 0;///大林吃的数量
        public static int smallNumber = 0;
        public static int TOTAL_NUMBER = 100;//做面包的总数
        public static int iCount=1; //计数器  统计
        
        public Kitchen(Brake brake)
        {
            this.brake=brake;
        }
        
        //做面包的方法
        public synchronized void make()
        {
            //判断锅是否已满
            if(Brake.BRAKE_NUM<10)
            {
                brake.insert(Kitchen.iCount);
                System.out.println("妈妈做完了第"+Kitchen.iCount+"个面包");
                Kitchen.iCount++;
                notifyAll();//唤醒2个儿子吃面包
            }
            else
            {
                System.out.println("锅已经有10个面包,等待儿子吃面包");
                try
                {
                    wait();
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }

        //吃面包的方法
        public  synchronized void eat()
        {
            //判断锅里是否为空
            if(Brake.BRAKE_NUM>0)
            {
                int temp=brake.remove();//获得吃掉面包的编号
                System.out.println(Thread.currentThread().getName()+"吃掉了第"+temp+"个面包");
            if(Thread.currentThread().getName().equals("大林"))
            {
                bigNumber++;
            }
            else
            {
                smallNumber++;
            }
            notifyAll();//唤醒妈妈继续做面包
            }
            else
            {
                System.out.println("面包吃完了 等待妈妈做面包");
                try
                {
                    wait();
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }
    ------------------------------------------------------------------

    //生产者类
    public class Productor implements Runnable{
        private Kitchen kitchen;
        public Productor(Kitchen kitchen)
        {
            this.kitchen=kitchen;
        }
        @Override
        public void run() {
            while(true)
            {
                if(Kitchen.iCount>100)//一百个面包做完
                {
                    break;                
                }else
                {
                    try {
                        kitchen.make();//妈妈不断做面包
                        Thread.sleep(150);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }        
            }
            System.out.println("100个面包做完了");
        }
        
    }

    ---------------------------------------------------------------

    //定义消费者类
    public class Consumer implements Runnable{
        private Kitchen kitchen;
        public Consumer(Kitchen kitchen)
        {
            this.kitchen=kitchen;
        }
        @Override
        public void run() {
            while(true)
            {
                //如何结束消费者线程
                if(Kitchen.iCount>100&&Brake.BRAKE_NUM<=0)
                {
                    break;
                }else
                {
                    try {
                        kitchen.eat();
                        Thread.sleep(100);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }        
        }
    }
    ----------------------------------------------------------------------------------------------

    //生产者消费者问题  java多线程实例项目
    public class TestBrake {
        public static void main(String[] args) {
            Brake brake=new Brake();
            Kitchen kitchen=new Kitchen(brake);
            Productor p=new Productor(kitchen);
            Consumer c1=new Consumer(kitchen);
            Consumer c2=new Consumer(kitchen);
            //妈妈线程
            Thread mother=new Thread(p,"妈妈");
            Thread bigSon=new Thread(c1,"大林");
            Thread smallSon=new Thread(c1,"小林");
            mother.start();
            bigSon.start();
            smallSon.start();
            
            try {
                mother.join();
                bigSon.join();
                smallSon.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("大林吃了:"+Kitchen.bigNumber+"个面包");
            System.out.println("小林吃了:"+Kitchen.smallNumber+"个面包");

        }
    }

    我希望有个如你一般的人, 如山间清爽的风, 如古城温暖的光, 只要最后是你就好。 今天, 你路过了谁? 谁又丢失了你呢?
  • 相关阅读:
    Python爬取微博热搜榜,将数据存入数据库
    Python爬取网站文章数据并存到数据库
    在自己的框架系统中使用tp类
    conda环境下pip install 无法安装到指定conda环境中(conda环境的默认pip安装位置)
    jupyter notebook 加入conda虚拟环境(指定conda虚拟环境)
    本地打包好的项目如何运行在docker中
    测试
    SQL Server创建dblink跨库查询
    深入浅析BIO、NIO、AIO
    JavaWeb_html_js_css_javaweb
  • 原文地址:https://www.cnblogs.com/smartwen666/p/7590921.html
Copyright © 2011-2022 走看看