Discussion of Activity 8.7 - Testing the storage tank

Sample solution

Code for class StorageTank:

public class StorageTank
{
   private int capacity;
   private int contents;

   public StorageTank(int cap)
   {
      capacity = cap;
   }

   public synchronized void in() throws Exception
   {
      while (contents == capacity)  // If the tank is full then wait.
      {
         wait();
      }

      contents++;
      System.out.println("One unit added - contents now " + contents);
      notifyAll(); // Signal that the state of the tank has changed.
   }

   public synchronized void out() throws Exception
   {
      while (contents == 0) // If the tank is empty then wait.
      {
         wait();
      }

      contents--;
      System.out.println("One unit subtracted - contents now " + contents);
      notifyAll(); // Signal that the state of the tank has changed.
   }
}