桌子上有一个盘子,每次只能放一个水果,爸爸只放苹果,儿子专门等着吃苹果;妈妈只放橘子,女儿专门等着吃橘子,只有盘子为空时,爸爸或妈妈才可以放水果,盘里有水果是,儿子或者女儿才能从盘子里取出
下面是我程序
- Java code
/** * 水果类 * 唯一标示 * @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); }}
下面是苹果生产者和苹果消费者类
- Java code
/** * 生产苹果的生产者 * * @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(); } }}