Guest User

Untitled

a guest
Apr 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. private int buffer[]; // Now an array of ints
  2. private int size; // Holds the length of the buffer
  3. public static int numItems; // Holds the number of items in the buffer
  4. private int getIndex;
  5. private int putIndex;
  6.  
  7. public TentBufferMonitor(int bufSize) {
  8. // Initialise the buffer to a specified size
  9. size = bufSize;
  10. buffer = new int[size];
  11.  
  12. // Initialise the indexes for getting and putting items (ints)
  13. getIndex = 0;
  14. putIndex = 0;
  15.  
  16.  
  17. }//TentBufferMonitor
  18.  
  19. public synchronized int get() throws InterruptedException {
  20. int value;
  21. while (numItems == 0) {
  22. wait(); // if necessary wait for buffer to have items
  23. }//while
  24. value = buffer[getIndex];
  25. getIndex = (getIndex + 1) % size;
  26. numItems--;
  27. notifyAll(); // notify any waiting producers that buffer has space
  28. return value;
  29. } // get
  30.  
  31. public synchronized void put(int value) throws InterruptedException {
  32. while (numItems == size) {
  33. wait();// wait for the buffer to have space
  34. System.out.println("Tent is currently at full capacity - Please wait!");
  35. }
  36. buffer[putIndex] = value; // place value in buffer
  37. putIndex = (putIndex + 1) % size;
  38. numItems++;
  39. notifyAll(); // notify the waiting consumer(s)
  40. Thread.sleep(3);
  41.  
  42. } // put
  43.  
  44. public static int numInTent(){
  45. return numItems;
  46. } //numInTent
  47.  
  48. ** Exit 2 : 1 have just left the tent **
  49.  
  50. ** Exit 1 : 1 have just left the tent **
  51.  
  52. Entrance 2: 1 person entered the tent. Waiting time for entering 8
  53.  
  54. Number of fans currently in tent: 0
  55.  
  56. Entrance 1: 1 person entered the tent. Waiting time for entering 4
  57.  
  58. Number of fans currently in tent: 0
  59.  
  60. Entrance 2: 2 people have entered the tent. Waiting time for entering 4
  61.  
  62. Number of fans currently in tent: 1
  63.  
  64. Entrance 1: 2 people have entered the tent. Waiting time for entering 4
  65.  
  66. Number of fans currently in tent: 2
  67.  
  68. ** Exit 2 : 2 have just left the tent **
  69.  
  70. ** Exit 1 : 2 have just left the tent **
Add Comment
Please, Sign In to add comment