日期:2014-05-20 浏览次数:20702 次
/** * 水果类 * 唯一标示 * @author Arthur * */ public class Fruits { private String name; public Fruits(String name){ this.name = name; } @Override public String toString(){ return name; } } /** * 橘子类 * @author Arthur * */ public class Orange extends Fruits { public Orange(String name) { super(name); } } /** * 苹果类 * @author Arthur * */ public class Apple extends Fruits { public Apple(String name) { super(name); } }
/** * 生产苹果的生产者 * * @author Arthur * */ public class ProducerApple implements Runnable { CriticalResources cr = null;// 封装一个临界资源对象,以便生产 public ProducerApple(CriticalResources cr) { super(); this.cr = cr; } @Override public void run() { synchronized (this) { if (cr.getFruites()[0] instanceof Orange||cr.getFruites()[0]!=null) { try { this.wait();//缓冲区满,该生产者等待 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } synchronized (this) { Fruits fruits = new Apple("苹果"); cr.push(fruits); System.out.println("苹果生产商生产了" + fruits); // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } } synchronized (cr.getCa()) { cr.getCa().notify(); } } } /** * 消费苹果的消费者 * * @author Arthur * */ public class ConsumerApple implements Runnable { private CriticalResources cr = null;// 封装一个临界资源对象,以便消费 public ConsumerApple(CriticalResources cr) { super(); this.cr = cr; } @Override public void run() { while (cr.getFruites()[0] != null && cr.getFruites()[0] instanceof Orange) { synchronized (this) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } synchronized (cr.getPa()) { Fruits fruits = cr.pop(); System.out.println("----苹果消费者消费了-------" + fruits); cr.getPa().notify(); } // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } } /** * 生产橘子的生产者 * * @author Administrator * */ public class ProducerOrange implements Runnable { CriticalResources cr = null; public ProducerOrange(CriticalResources cr) { super(); this.cr = cr; } @Override public void run() { synchronized(this){ if(cr.getFruites()[0] instanceof Apple||cr.getFruites()[0]!=null){ try { this.wait();//该生产这等待 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } while (cr.getFruites()[0] ==null) { synchronized (this) { Fruits fruits = new Orange("橘子"); cr.push(fruits); System.out.println("橘子生产商生产了" + fruits); } // } synchronized(cr.getCo()){ cr.getCo().notify();//橘子消费者唤醒该橘子生产者 } } } /** * 消费橘子的消费者 * * @author Administrator * */ public class ConsumerOrange implements Runnable { private CriticalResources cr = null;// 封装一个临界资源对象,以便消费 public ConsumerOrange(CriticalResources cr) { super(); this.cr = cr; } @Override public void run() { //如果缓冲区是苹果 while (cr.getFruites()[0]!= null && cr.getFruites()[0] instanceof Apple) { synchronized (this) { try { this.wait();//该消费者等待 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } synchronized (cr.getPo()) { Fruits fruits = cr.pop(); System.out.println("----橘子消费者消费了-------" + fruits); cr.getPo().notify(); } } }