001 package stringtransformers; 002 003 import cs101.util.queue.StringQueue; 004 import java.util.Vector; 005 006 public class BlockingStringQueue implements StringQueue 007 { 008 private Vector v = new Vector(); 009 private Object qReady = new Object(); 010 011 public synchronized void enQ ( String s ) 012 { 013 this.v.addElement( s ); 014 // this.qReady.notify(); 015 this.notify(); 016 } 017 018 public synchronized String deQ () 019 { 020 while ( this.isEmptyQ() ) 021 { 022 try 023 { 024 // this.qReady.wait(); 025 this.wait(); 026 } 027 catch (InterruptedException e ) 028 { 029 } 030 } 031 032 Object o = this.v.elementAt( 0 ); 033 this.v.removeElementAt( 0 ); 034 if ( ! ( o instanceof String ) ) 035 { 036 throw new RuntimeException( "Found a non-String in StringQueue!"); 037 } 038 return (String) o; 039 } 040 041 public synchronized boolean isEmptyQ () 042 { 043 return this.v.isEmpty(); 044 } 045 } 046