00001 class BoundedBuffer 00002 { 00003 java.lang.Object[] buffer; 00004 int tail; 00005 int head; 00006 int bound; 00007 00008 public BoundedBuffer(int b) 00009 { 00010 this.bound = b; 00011 this.buffer = new java.lang.Object[this.bound]; 00012 this.head = 0; 00013 this.tail = (this.bound - 1); 00014 } 00015 00016 public synchronized void add (java.lang.Object o) 00017 { 00018 while (this.tail == this.head) 00019 { 00020 try { 00021 this.wait(); 00022 } 00023 catch (java.lang.InterruptedException ex) { 00024 } 00025 } 00026 this.buffer[this.head] = o; 00027 this.head = ((this.head + 1) % this.bound); 00028 this.notifyAll(); 00029 } 00030 00031 public synchronized java.lang.Object take () 00032 { 00033 while (this.isEmpty()) 00034 { 00035 try { 00036 this.wait(); 00037 } 00038 catch (java.lang.InterruptedException ex) { 00039 } 00040 } 00041 this.tail = ((this.tail + 1) % this.bound); 00042 this.notifyAll(); 00043 return this.buffer[this.tail]; 00044 } 00045 00046 public synchronized boolean isEmpty () 00047 { 00048 return (this.head == ((this.tail + 1) % this.bound)); 00049 } 00050 }