import java.util.Collection; import java.util.Comparator; import java.util.PriorityQueue; import java.util.SortedSet; /** * The CrusherQueue is a Queue base on the PriorityQueue class to be consumed by the Crusher. * This class is Thread safe. * * @author Ahmed Messaoud * @see Crusher#q * @see PriorityQueue */ public class CrusherQueue extends PriorityQueue { private boolean available = true; /* (non-Javadoc) * @see java.util.PriorityQueue#add(java.lang.Object) * @see Truck */ public synchronized boolean add(Truck t){ if( !available ){ try{ wait(); }catch( Exception e ){} } available = false; //check size, if there is waiting, then advise truck to idle if( size() > 0){ try{ t.startIdleling(); }catch(AlreadyIdlelingException e){} } available = true; notify(); return super.add(t); } /* (non-Javadoc) * @see java.util.PriorityQueue#poll() * @see Truck */ public synchronized Truck poll(){ if( !available ){ try{ wait(); }catch( Exception e ){} } available = false; //check if truck was idleling, if it was, stop the idleling if( super.peek() != null ){ if( super.peek().isIdleling() ){ try{ super.peek().stopIdleling(); }catch(NotIdlelingException e){} } } available = true; notify(); return super.poll(); } }