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();
}
}
}