多线程のPV操作(厨师->【馒头】->消费者)过程

本文详细阐述了多线程环境下的PV操作原理,通过厨师与消费者的角色模拟,实现了生产与消费的同步机制。包括面包类、盘子类、厨师类和消费者类的设计与实现,以及主程序的调度逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.cwh.PVdemo1;

import java.io.Serializable;


/**
 * 多线程のPV操作(厨师->【馒头】->消费者)过程
 * @author(创建者) cwh
 * @datetime(创建时间) 2015-8-11 - 下午06:19:15
 * @describe(描述) 面包类
 */
public class bread{
    private int id ;
    public bread(int id) {
        this.id = id;
    }
    public String toString(){
        return "面包:"+getId();
    }
    public int getId(){
        return this.id;
    }
}


package com.cwh.PVdemo1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 多线程のPV操作(厨师->【馒头】->消费者)过程
 * @author(创建者) cwh
 * @datetime(创建时间) 2015-8-11 - 下午06:29:21
 * @describe(描述) 盘子类
 */
public class dish {
    private static dish dishobj =null;
    private int MaxSize = 10;
    private int count = 0;
    private ArrayList list = new ArrayList();  
    /**
     * 把厨师生产的面包加入到盘子中(模拟)
     * @param need
     */
    public void push(int need){
        synchronized(this){
            if (list.size()>=MaxSize) { //当盘子装满了10个包子的时候
                try {
                    this.wait();         //让盘子不能进行任何操作(阻塞),实现厨师和消费者同步的作用
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notifyAll();  //唤醒所有其他线程
            for(int x = 0;x<need;x++){  
                list.add(new bread(x));
            }
            count = list.size();
            System.out.println("【厨师】 要把"+need+"个包子放在盘子中"+"\t\t"+"当前盘子剩余包子:"+getCount());
        }
    }
    
    /**
     * 把消费者吃了的面包移除盘子
     */
    public void pop(int eat){
        synchronized(this){
            System.out.println("==========这次要吃"+eat+"个============");
            if(list.size()<eat){  //当盘子里面没有面包的时候
                System.out.println("【【警告:!!】】========盘子的面包不够你吃啦!先等厨师加面包先吧。=========【【警告:!!】】");
                try {
                    this.wait();//让盘子不能进行任何操作(阻塞),实现厨师和消费者同步的作用
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notifyAll();
            for(int x = 0;x<eat;x++)
            list.remove(x);
            count = list.size();
            System.out.println("【消费者】 要吃"+eat+"个包子"+"\t\t"+"当前盘子剩余包子:"+getCount());
        }
    }
    
    public int getCount() {
        return count;
    }
    /*懒汉单例--设计模式*/
    private dish(){}//私有构造方法
    public static dish getInstance(){
        if (dishobj == null) {
                dishobj = new dish();
                return dishobj;
        }else{
            return dishobj;
        }
    }
}


package com.cwh.PVdemo1;
/**
 * 多线程のPV操作(厨师->【馒头】->消费者)过程
 * @author(创建者) cwh
 * @datetime(创建时间) 2015-8-11 - 下午06:19:08
 * @describe(描述) 厨师类
 */
public class cook implements Runnable {
    private dish dishobj = dish.getInstance();
    private int need;
    /**
     * 厨师需要生产的面包数
     * @param need
     */
    public cook(int need) {
        this.need=need;
    }
    @Override
    public void run() {
        dishobj.push(this.need);
        try {
            Thread.sleep(1*1000);  //厨师生产面包需要6S
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

package com.cwh.PVdemo1;
/**
 * 多线程のPV操作(厨师->【馒头】->消费者)过程
 * @author(创建者) cwh
 * @datetime(创建时间) 2015-8-11 - 下午06:20:47
 * @describe(描述) 消费者
 */
public class customer implements Runnable {
    private dish dishobj = dish.getInstance();
    private int eat;
    /**
     * 厨师需要生产的面包数
     * @param eat
     */
    public customer(int eat) {
        this.eat=eat;
    }
    @Override
    public void run() {
        dishobj.pop(this.eat);
        try {
            Thread.sleep(6*1000);  //顾客吃面包需要1S
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

package com.cwh.PVdemo1;
/**
 * 多线程のPV操作(厨师->【馒头】->消费者)过程
 * @author(创建者) cwh
 * @datetime(创建时间) 2015-8-11 - 下午06:21:23
 * @describe(描述) 主程序
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        cook[] arry = new cook[5];
        customer[] arry2 =new customer[7];
        for(int i=1;i<=5;i++){  //5个级别的厨师。每个级别对应要生产面包数
//            System.out.println("new cook"+i);
            cook cookobj = new cook(i);
             new Thread(cookobj).start();
        }
        for(int i=1;i<=4;i++){ //4个顾客。每个顾客吃的份额也不同
//            System.out.println("new customer"+i);
            customer customerobj = new customer(i);
            new Thread(customerobj).start();
        }
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值