Source of BoundedQueueWithGuard.java


  1: public class BoundedQueueWithGuard extends BoundedQueue {
  2:     public BoundedQueueWithGuard(int size) {
  3:         super(size);
  4:     }
  5: 
  6:     synchronized public boolean isEmpty() {
  7:         return super.isEmpty();
  8:     }
  9: 
 10:     synchronized public boolean isFull() {
 11:         return super.isFull();
 12:     }
 13: 
 14:     synchronized public int getCount() {
 15:         return super.getCount();
 16:     }
 17: 
 18:     synchronized public void put(Object obj) {
 19:         try {
 20:             while (isFull()) {
 21:                 wait();
 22:             }
 23:         } catch (InterruptedException e) {}
 24:         super.put(obj);
 25:         notify();
 26:     }
 27: 
 28:     synchronized public Object get() {
 29:         try {
 30:             while (isEmpty()) {
 31:                 wait();
 32:             }
 33:         } catch (InterruptedException e) {}
 34:         Object result = super.get();
 35:         notify();
 36:         return result;
 37:     }
 38: 
 39:     public static void main(String args[]) {
 40:         BoundedQueueWithGuard queue =
 41:             new BoundedQueueWithGuard(5);
 42:         new Producer(queue, 15).start();
 43:         new Consumer(queue, 15).start();
 44:     }
 45: }