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 Bandera.assert((b > 0) ); 00011 this.bound = b; 00012 this.buffer = new java.lang.Object[this.bound]; 00013 this.head = 0; 00014 this.tail = (this.bound - 1); 00015 } 00016 00017 public synchronized void add (java.lang.Object o) 00018 { 00019 while (this.tail == this.head) 00020 { 00021 try { 00022 this.wait(); 00023 } 00024 catch (java.lang.InterruptedException ex) { 00025 } 00026 } 00027 this.buffer[this.head] = o; 00028 this.head = ((this.head + 1) % this.bound); 00029 this.notifyAll(); 00030 Bandera.assert((this.buffer[(this.head - 1)] == o) ); 00031 } 00032 00033 public synchronized java.lang.Object take () 00034 { 00035 while (this.isEmpty()) 00036 { 00037 try { 00038 this.wait(); 00039 } 00040 catch (java.lang.InterruptedException ex) { 00041 } 00042 } 00043 this.tail = ((this.tail + 1) % this.bound); 00044 this.notifyAll(); 00045 return this.buffer[this.tail]; 00046 } 00047 00048 public synchronized boolean isEmpty () 00049 { 00050 return (this.head == ((this.tail + 1) % this.bound)); 00051 } 00052 }