知识点:生产者消费者模型
注意:一个生产者一个消费者的情况
需求:生产一个消费一个
步骤:
1.让生产者线程和消费者线程 共同操作同一个电脑对象(资源)
2.让两个产品来回切换(目的是放大第一个步骤的问题)
加锁,防止脏数据的出现
3.生产一个消费一个
public class computer{ //定义属性 private String brand; private double price; private boolean isStore;//判断是否有库存 public computer{} @Override public computer(String brand, double price){ this.brand=brand; this.price=price; } public void getBrand(){return brand;} public void setBrand(String brand){this.brand=brand;} public void getPrice(){return price;} public void setPrice(double price){this.price=price;} public void isStore(){return isStore;} public void setStore(Boolean store){isStore=store;} }
public class Producer{ private Computer computer; public Producer(Computer computer){this.computer=computer;} @Override public void run(){ boolean flag=true; while(ture){ synchronized(computer){ try{ computer.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } if(flag){ computer.setBrand("华硕"); computer.setPrice(6999); }else{ computer.setBrand("联想"); computer.setPrice(7999); } flag!=flag;//置反 computer.setStore(true);//设置成有库存 computer.notify();//唤醒 } } }
public class Consumer{ private Computer computer; public Consumer(Computer computer){ this.computer=computer; } @Override public void run(){ while(true){ synchronized(computer){ if(!computer.isStore){ try{ computer.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } System.out.println(computer.getBrand()+"----"+computer.getPrice()); computer.setStore(false); computer.notify(); } } } }
public class Test{ public static void main(String[] args){ Computer computer=new Computer(); Producer p=new Producer(computer); Consunmer c=new Consunmer(computer); p.start(); c.start(); } }
* 知识点:生产者消费者模型
*
* 注意:多个生产者多个消费者的情况
*
* 需求:生产一个消费一个
*
* 步骤:
* 1.让生产者线程和消费者线程 共同操作同一个电脑对象(资源)
* 2.让两个产品来回切换(目的是放大第一个步骤的问题)
* 加锁,防止脏数据的出现
* 3.生产一个消费一个
public class Computer{ private String brand; private double price; private boolean isStore; public Computer(String brand,double price){ this.brand=brand; this.price=price; } public void getBrand(){return brand;} public void setBrand(String brand){this.brand=brand;} public void getPrice(){return price;} public void setPrice(double price){this.price=price;} public void isStore(){return isStore;} public void setStore(boolean store){isStore=store;} }
public class Producer{ private Computer computer; public Producer(Computer computer){this.computer=computer;} @Override public void run(){ int flag=true; while(true){ synchronized(computer){ while(computer.isStore){ try{ computer.wait();//等待 }catch(InterruptedException e){ e.printStackTrace(); } } if(flag){ computer.getBrand("机械革命师"); computer.getPrice(6999); }else{ computer.getBrand("华硕"); computer.getPrice(9999); } flag!=flag; computer.setStore(true); computer.notifyAll();//唤醒 } } } }
public class Consunmer{ private Computer computer; public Consunmer(Computer computer){ this.computer=computer; } @Override public void run(){ while(true){ synchronized(computer){ while(!computer.isStore){ try{ computer.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } System.out.println(computer.getBrand()+"-------"+computer.getPrice()); computer.setStore(false); computer.notifyAll(); } } } }
public class Test{ public static void main(String[] args){ Computer computer=new Computer(); Consunmer c1=new Consunmer(computer); Producer p1=new Producer(computer); Consumer c2=new Consunmer(computer); Producer p2=new Producer(computer); c1.start(); c2.start(); p1.start(); p2.start(); } }
-
知识点:仓储模型
-
注意:一个生产者和一个消费者
public class Cake{ private String brand; private String DataTime; public Cake(){} public Cake(String brand,String DataTime){ this.brand=brand; this.DataTime=DataTime; } public void getBrand(){ruturn brand;} public void setBrand(String brand){this.brand=brand;} public void getDataTime(){ruturn DataTime;} public void setDataTime(String DataTime){this.DataTime=DataTime;} }
public class Producer{ private Store store; public Producer(Store store){ this.store=store; } @Override public void run(){ SimpleDateFromat sdf=new SimpleDateFromat("yyyy-MM-dd HH:mm:ss"); while(true){ Cake cake=new Cake("樱樱面包",sdf.format(new Date())); store.push(cake); } } }
public class Consumer{ private Store store; public Consumer(Store store){ this.store=store; } @Override public void run(){ while(true){ store.pop(); } } }
public class Store{ private LinkedList<Cake> list=new LinkedList<>(); private int maxCapacity=20; private int currentCapacity; public void push(Cake cake){ synchronized(this){ if(currentCapacity>=maxCapacity){ try{ this.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } list.add(cake); currentCacity++; System.out.println("入库,当前容量位:"+currentCapacity); this.notify();//通过,对象监视器,唤醒,被等待的线程 } } public void pop(){ synchronized(this){ if(currentCapacity<=0){ try{ this.wait(); }catch(InterruptedException e){ e.printStackTrace(); } } Cake cake=list.removeFirst(); currentCapacity--; System.out.println("出库,当前容量位:"+currentCapacity); this.notify(); } } }
public class Test{ /** * 知识点:仓储模型 * * 注意:一个生产者一个消费者的情况 */ Store store =new Store(); Producer p=new Producer(store); Consumer c=new Consumer(store); p.start(); c.start(); }
知识点:仓储模型
注意:多个生产者多个消费者的情况
//蛋糕类 public class Cake { private String brand; private String dataTime; public Cake() { } public Cake(String brand, String dataTime) { this.brand = brand; this.dataTime = dataTime; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getDataTime() { return dataTime; } public void setDataTime(String dataTime) { this.dataTime = dataTime; } @Override public String toString() { return "Cake [brand=" + brand + ", dataTime=" + dataTime + "]"; } }
public class Consumer extends Thread{ private Store store; public Consumer(Store store) { this.store = store; } @Override public void run() { while(true){ store.pop(); } } }
public class Producer extends Thread{ private Store store; public Producer(Store store) { this.store = store; } @Override public void run() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while(true){ Cake cake = new Cake("桃李面包", sdf.format(new Date())); store.push(cake); } } }
public class Store { //蛋糕容器 private LinkedList<Cake> list = new LinkedList<>(); //最大容量 private int maxCapacity = 20; //当前容量 private int currentCapacity; //currentCapacity - 22 //入库 public void push(Cake cake){ synchronized (this) { while(currentCapacity >= maxCapacity){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } list.add(cake); currentCapacity++; System.out.println("入库,当前容量为:" + currentCapacity); this.notifyAll(); } } //出库 public void pop(){ synchronized (this) { while(currentCapacity <= 0){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Cake cake = list.removeFirst(); currentCapacity--; System.out.println("出库,当前容量为:" + currentCapacity + " -- " + cake); this.notifyAll(); } } }
public class Test01 { public static void main(String[] args) { /** * 知识点:仓储模型 * * 注意:多个生产者多个消费者的情况 */ Store store = new Store(); Producer p1 = new Producer(store); Producer p2 = new Producer(store); Consumer c1 = new Consumer(store); Consumer c2 = new Consumer(store); p1.start(); p2.start(); c1.start(); c2.start(); } }