background image

 
while(tank==null){
 //No tank yet, let's wait.
  try{
 wait();
 }catch(InterruptedException e){}
 }
   Tank retValue=tank.
 tank=null; //Clear tank.
 return retValue;
  }
 }
  public ProducerThread extends Thread{
 //Shared TankContainer
  private TankContainer container;
 public ProducerThread(TankContainer container){
 this.container=container;
  ...

 public void run(){
 while(true){

 Tank tank=produceTank();

   container.putTank(tank); 
   }


public ConsumerThread extends Thread{ 
//Shared TankContainer 
private TankContainer container;
public ConsumerThread(TankContainer container){ 
this.container=container;

...
public void run(){
while(true){
 Tank tank=container.getTank();
 consumeTank(tank);
    }
     }
  }
 public class ProducerConsumer{