public class ProducerConsumer { public static void main(String[] args) { SyncStack ss = new SyncStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); new Thread(p).start(); new Thread(c).start(); } } class WoTou { int id; WoTou(int id) { this.id = id; } public String toString() { return "WoTou : " + id; } } class SyncStack { int index = 0; WoTou[] arrWT = new WoTou[6]; public synchronized void push(WoTou wt) { while (index == arrWT.length) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); arrWT[index] = wt; System.out.println("set: " + arrWT[index]); index++; } public synchronized WoTou pop() { while (index == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); index--; System.out.println("get :" + arrWT[index]); return arrWT[index]; } } class Producer implements Runnable { SyncStack ss = null; Producer(SyncStack ss) { this.ss = ss; } public void run() { for (int i = 0; i < 20; i++) { WoTou wt = new WoTou(i); ss.push(wt); try { Thread.sleep((int) (Math.random() * 200)); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { SyncStack ss = null; Consumer(SyncStack ss) { this.ss = ss; } public void run() { for (int i = 0; i < 20; i++) { WoTou wt = ss.pop(); try { Thread.sleep((int) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
下面是自己的demo:
import org.apache.log4j.Logger; public class Consumer implements Runnable{ private Container container; Logger log = Logger.getLogger(Consumer.class); public Consumer(Container container){ this.container = container; } @Override public void run() { for(int i=0;i<10;i++){ try { Thread.sleep((int) (Math.random() * 1000)); container.get(); //log.info("消费了:"+container.get()); }catch (Exception e) { e.printStackTrace(); } } } }
import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; public class Container { private List<Food> container; Logger log = Logger.getLogger(Container.class); public Container(List<Food> container){ this.container = container; } public synchronized Food get() throws Exception{ if(container == null){ container = new ArrayList<Food>(); } while(container.isEmpty()){ this.wait(); } Food food = container.remove(container.size()-1); log.info("get food:"+food+",container:"+container); this.notifyAll(); return food; } public synchronized void set(Food food) throws Exception{ if(container == null){ container = new ArrayList<Food>(); } while(container.size() >= 10){ this.wait(); } log.info("set food: "+food+",container:"+container); container.add(food); this.notifyAll(); } }
public class Food { private int id; public Food(int id){ this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Food [id=" + id + "]"; } }
import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ProducerConsumerTest { public static void main(String[] args) { List<Food> list = new ArrayList<Food>(); Container container = new Container(list); Producer p = new Producer(container); Consumer c = new Consumer(container); //new Thread(p).start(); //new Thread(p2).start(); //new Thread(c).start(); ExecutorService pool = Executors.newFixedThreadPool(2); pool.execute(p); pool.execute(c); } }
相关推荐
马士兵老师spring框架学习笔记
马士兵JAVA笔记(全).docx
马士兵oracle笔记,浅显易懂。
jvm java虚拟机 调优 马士兵 笔记 让你对java虚拟机调优有初步的认识
linux 马士兵笔记
马士兵老师mybatis相关学习笔记
马士兵Spring课堂笔记(超级详细版).pdf
马士兵Shopping项目源代码,马士兵Shopping项目源代码,马士兵Shopping项目源代码,马士兵Shopping项目源代码,
马士兵老师HashMap学习笔记
马士兵j2se源码和ppt马士兵j2se源码和ppt马士兵j2se源码和ppt马士兵j2se源码和ppt马士兵j2se源码和ppt
马士兵 struts2 操作手册
马士兵Spring课堂笔记(详细版) 马士兵spring视频笔记
java 马士兵 网上商城 源代码 可以运行的购物系统
马士兵hibernate文档.doc
马士兵多线程训练营笔记
一.java内存结构 2 二 垃圾收集算法: 3 三 JVM参数 4 四 JVM的垃圾回收集器 7 五 常用参数设置 7
是自己根据马士兵的NewShopping模仿着一步步做的,对项目的界面进行了美化,更加漂亮美观,并对视频中项目一些细节不足的地方进行了一些修正,并添加了部分功能进行完善,但是对于视频中报表和文件上传的功能我并...
马士兵Springboot课堂笔记
马士兵多线程训练营上课笔记