首页 技术 正文
技术 2022年11月15日
0 收藏 713 点赞 3,996 浏览 3958 个字

创建四个类
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模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902