| 
		
			| 
	
	
		
			Goods.java  package com.bebig.producerconsumer; 
  
  //商品 
   public class Goods  { 
  private int id; 
  
   public Goods(int id)  { 
  this.setId(id); 
  } 
  
   public void setId(int id)  { 
  this.id = id; 
  } 
  
   public int getId()  { 
  return id; 
  } 
  } 
 
Cart.java
  package com.bebig.producerconsumer; 
  
  //购物车 
   public class Cart  { 
  private final int MAXVALUE = 6;// 购物车里最多能存放的商品数量 
  private int index = 0; 
  private Goods[] goods = new Goods[MAXVALUE]; 
  
   public synchronized void push(String name, Goods g)  { 
   while (index == goods.length)  { 
  // 购物车里满了就不要放物品进来了 
   try  { 
  System.out.println("----购物车里的商品已满,停止生产."); 
  this.wait(); 
   } catch (InterruptedException e)  { 
  e.printStackTrace(); 
  } 
  } 
  this.notifyAll(); 
  this.goods[index] = g; 
  index++; 
  System.out.println("生产者" + name + "向购物车里放置了一件商品."); 
  
  } 
  
   public synchronized Goods pop(String name)  { 
   while (0 == index)  { 
  // 购物车里空了就不要取物品出去了 
   try  { 
  System.out.println("----购物车里的商品已空,停止消费."); 
  this.wait(); 
   } catch (InterruptedException e)  { 
  e.printStackTrace(); 
  } 
  } 
  this.notifyAll(); 
  index--; 
  System.out.println("消费者" + name + "从购物车取走了一件商品."); 
  return this.goods[index]; 
  } 
  
  }
Producer.java
  package com.bebig.producerconsumer; 
  
  //生产者 
   public class Producer implements Runnable  { 
  private Cart cart = null; 
  private String name; 
  
   public Producer(Cart c, String name)  { 
  this.cart = c; 
  this.setName(name); 
  } 
  
  @Override 
   public void run()  { 
   for (int i = 1; i < 10; i++)  { 
  cart.push(this.getName(), new Goods(i)); 
   try  { 
  Thread.sleep((int) (Math.random() * 1000)); 
   } catch (InterruptedException e)  { 
  e.printStackTrace(); 
  } 
  } 
  } 
  
   public void setName(String name)  { 
  this.name = name; 
  } 
  
   public String getName()  { 
  return name; 
  } 
  }
Consumer.java
 测试代码: package com.bebig.producerconsumer; 
  
  //消费者 
   public class Consumer implements Runnable  { 
  private Cart cart = null; 
  private String name; 
  
   public Consumer(Cart c, String name)  { 
  this.name = name; 
  this.cart = c; 
  } 
  
  @Override 
   public void run()  { 
   for (int i = 1; i < 10; i++)  { 
  cart.pop(this.getName()); 
   try  { 
  Thread.sleep((int) (Math.random() * 1000)); 
   } catch (InterruptedException e)  { 
  e.printStackTrace(); 
  } 
  } 
  } 
  
   public void setName(String name)  { 
  this.name = name; 
  } 
  
   public String getName()  { 
  return name; 
  } 
  
  } package com.bebig.producerconsumer; 
  
  //测试 
   public class ProducerAndConsumer  { 
  
   /** *//** 
  * @param args 
  */ 
   public static void main(String[] args)  { 
  Cart c = new Cart(); 
  
  Producer p1 = new Producer(c, "Don"); 
  Consumer c1 = new Consumer(c, "Scan"); 
  Producer p2 = new Producer(c, "txc"); 
  Consumer c2 = new Consumer(c, "Sun"); 
  new Thread(p1).start(); 
  new Thread(c1).start(); 
  new Thread(p2).start(); 
  new Thread(c2).start(); 
  
  } 
  
  } 
 
测试结果(随机显示):
  生产者Don向购物车里放置了一件商品. 
  生产者txc向购物车里放置了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  ----购物车里的商品已空,停止消费. 
  生产者txc向购物车里放置了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  ----购物车里的商品已空,停止消费. 
  生产者txc向购物车里放置了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  生产者Don向购物车里放置了一件商品. 
  生产者Don向购物车里放置了一件商品. 
  生产者txc向购物车里放置了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  生产者Don向购物车里放置了一件商品. 
  生产者txc向购物车里放置了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  生产者txc向购物车里放置了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  生产者txc向购物车里放置了一件商品. 
  生产者Don向购物车里放置了一件商品. 
  生产者txc向购物车里放置了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  生产者Don向购物车里放置了一件商品. 
  生产者txc向购物车里放置了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  ----购物车里的商品已空,停止消费. 
  生产者Don向购物车里放置了一件商品. 
  消费者Sun从购物车取走了一件商品. 
  ----购物车里的商品已空,停止消费. 
  生产者Don向购物车里放置了一件商品. 
  消费者Scan从购物车取走了一件商品. 
  ----购物车里的商品已空,停止消费. 
  生产者Don向购物车里放置了一件商品. 
  消费者Sun从购物车取走了一件商品.     
	    
    
 |  | 
			
				| CALENDER 
	|  |  | 日 | 一 | 二 | 三 | 四 | 五 | 六 | 
|---|
 | 28 | 29 | 30 | 1 | 2 | 3 | 4 |  | 5 | 6 | 7 | 8 | 9 | 10 | 11 |  | 12 | 13 | 14 | 15 | 16 | 17 | 18 |  | 19 | 20 | 21 | 22 | 23 | 24 | 25 |  | 26 | 27 | 28 | 29 | 30 | 31 | 1 |  | 2 | 3 | 4 | 5 | 6 | 7 | 8 |  |  常用链接留言簿随笔分类随笔档案文章分类文章档案新闻档案相册搜索最新评论
	阅读排行榜评论排行榜Powered By: 博客园
 模板提供:沪江博客
 
 
 |