daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Jul 21st, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Martin Jeffers - 59351795
  2.    Edward Ryan - 10335899
  3.    Andrew O'Hart - 10386963
  4.  */
  5.  
  6.  
  7.  
  8. class BoundedBuffer
  9. {
  10.     private int nextIn = 0;
  11.     private int nextOut = 0;
  12.    
  13.     private int bufferSize = 0;
  14.     private int occupied = 0;
  15.     private int delta = 0;
  16.    
  17.     private int Ins = 0;       
  18.     private int Outs = 0;      
  19.    
  20.     private boolean dataAvailable = false;
  21.     private boolean roomAvailable = true;
  22.  
  23.     private int [] buffer;
  24.     private long [] time;      
  25.     private long totalTime;    
  26.     private double averageTime;
  27.  
  28.     private int bufferCount = 0;
  29.  
  30.     private long start;        
  31.     private long end;          
  32.  
  33.     BoundedBuffer(int size0)
  34.     {
  35.         bufferSize = size0;
  36.         buffer = new int[bufferSize];
  37.         time = new long[bufferSize];
  38.     }
  39.  
  40.     public void start() //  code taken from main and converted into a setter method
  41.     {
  42.         start = System.currentTimeMillis();
  43.         end = start + 60000;
  44.     }
  45.  
  46.     public long endTime() // getter method
  47.     {
  48.         return end;
  49.     }
  50.  
  51.     public synchronized void insertItem(int number)     // added public
  52.     {
  53.         while(!roomAvailable)
  54.         {
  55.             try
  56.             {
  57.                 wait();
  58.             }
  59.             catch (InterruptedException e)
  60.             {
  61.             }
  62.         }
  63.  
  64.         buffer[nextIn] = number;
  65.         time[nextIn] = System.currentTimeMillis();
  66.        
  67.         Ins++;
  68.         bufferCount++;
  69.        
  70.         nextIn = (nextIn+1) % bufferSize;
  71.  
  72.         if(bufferSize == bufferCount)
  73.             roomAvailable = false;
  74.         else
  75.             dataAvailable = true;
  76.             notifyAll();
  77.     }
  78.  
  79.     public synchronized void removeItem()       // added public
  80.     {
  81.         while(!dataAvailable)
  82.         {
  83.             try
  84.             {
  85.                 wait();
  86.             }
  87.             catch (InterruptedException e)
  88.             {
  89.             }
  90.         }
  91.  
  92.         totalTime = totalTime + System.currentTimeMillis() - time[nextOut];
  93.  
  94.         nextOut = (nextOut+1) % bufferSize;
  95.  
  96.         Outs++;
  97.         bufferCount--;
  98.  
  99.         if(bufferCount == 0)
  100.             dataAvailable = false;
  101.         else
  102.             roomAvailable = true;
  103.             notifyAll();
  104.     }
  105.  
  106.     public synchronized void getBufferStats()       // added public
  107.     {
  108.         delta = Ins-Outs-bufferCount;
  109.         System.out.println("Delta = " + delta + " Occupied = " + bufferCount);
  110.     }
  111.  
  112.     public void printAverageTime() // code taken from main
  113.     {
  114.         averageTime = (((double)totalTime) / (Outs));
  115.         System.out.println("Average wait time : " + averageTime);
  116.     }
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////////////
  119. //////////////////////////////////////////////////////////////////////////////////////
  120. class Watcher extends Thread
  121. {
  122.     private BoundedBuffer buff;
  123.  
  124.     Watcher(BoundedBuffer buff0)
  125.     {
  126.         buff = buff0;
  127.     }
  128.  
  129.     public void run()
  130.     {
  131.         while(System.currentTimeMillis() < buff.endTime())  //changed from global variable 'end' in main - to getter method in buffer class
  132.         {
  133.             buff.getBufferStats();      // moved from after try block to before
  134.                                         // sometimes  at the end of the program watcher would sleep and re-awaken
  135.                                         // to print this line between the "good-bye" lines
  136.  
  137.             try
  138.             {
  139.                 Watcher.sleep(1000);
  140.             }
  141.             catch (InterruptedException e)
  142.             {
  143.             }
  144.         }
  145.     }
  146. }
  147. ///////////////////////////////////////////////////////////////////////////////////////
  148. //////////////////////////////////////////////////////////////////////////////////////
  149. class Producer extends Thread
  150. {
  151.     private int randomNum;
  152.     private BoundedBuffer buff;
  153.  
  154.     Producer(BoundedBuffer buff0)
  155.     {
  156.         buff = buff0;
  157.     }
  158.  
  159.     public void run()
  160.     {
  161.         while(System.currentTimeMillis() < buff.endTime())  //changed from global variable 'end' in main - to getter method in buffer class
  162.         {
  163.             randomNum = (int)((Math.random()) * 101);
  164.             buff.insertItem(randomNum);
  165.  
  166.             randomNum = (int)((Math.random()) * 101);
  167.  
  168.             try
  169.             {
  170.                 Producer.sleep(randomNum);
  171.             }
  172.             catch (InterruptedException e)
  173.             {
  174.             }
  175.         }
  176.     }
  177. }
  178. ///////////////////////////////////////////////////////////////////////////////////////
  179. //////////////////////////////////////////////////////////////////////////////////////
  180. class Consumer extends Thread
  181. {
  182.     private int randomNum;
  183.     private BoundedBuffer buff;
  184.  
  185.     Consumer(BoundedBuffer buff0)
  186.     {
  187.         buff = buff0;
  188.     }
  189.  
  190.     public void run()
  191.     {
  192.         while(System.currentTimeMillis() < buff.endTime())  //changed from global variable in main - to getter method in buffer class
  193.         {
  194.            
  195.             buff.removeItem();
  196.  
  197.             randomNum = (int)((Math.random()) * 101);
  198.  
  199.             try
  200.             {
  201.                 Consumer.sleep(randomNum);
  202.             }
  203.             catch (InterruptedException e)
  204.             {
  205.             }
  206.         }
  207.     }
  208. }
  209. ///////////////////////////////////////////////////////////////////////////////////////
  210. //////////////////////////////////////////////////////////////////////////////////////
  211. public class Assignment1
  212. {
  213.     public static void main(String [] args)
  214.     {
  215.         BoundedBuffer buff = new BoundedBuffer(Integer.parseInt(args[0]));
  216.         Thread Producer0 = new Producer(buff);
  217.         Thread Consumer0 = new Consumer(buff);
  218.         Thread Watcher0 = new Watcher(buff);
  219.  
  220.         buff.start();           //sets buffers start and end time
  221.         Producer0.start();
  222.         Consumer0.start();
  223.         Watcher0.start();
  224.  
  225.         try
  226.         {
  227.             Producer0.join();
  228.             System.out.println("Good-Bye from Producer.");
  229.  
  230.             Consumer0.join();
  231.             System.out.println("Good-Bye from Consumer.");
  232.  
  233.             Watcher0.join();
  234.             System.out.println("Good-Bye from Watcher.");
  235.         }
  236.         catch (InterruptedException e)
  237.         {
  238.         }
  239.  
  240.         buff.printAverageTime();    // prints average time
  241.     }
  242. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top