import java.util.concurrent.*; /** * The Queue to be consumed by the Shovel. It is based on the ConcurrentLinkedQueue and made specifically for the Shovel class. * This class is Thread safe. * * @author Ahmed Messaoud * @see ConcurrentLinkedQueue * @see Shovel */ public class ShovelQueue extends ConcurrentLinkedQueue { private int size; private boolean available = true; public ShovelQueue(){ super(); size=0; } /* (non-Javadoc) * @see java.util.concurrent.ConcurrentLinkedQueue#add(java.lang.Object) */ 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){} } size++; available = true; notify(); return super.add(t); } /* (non-Javadoc) * @see java.util.concurrent.ConcurrentLinkedQueue#poll() */ 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){} } size--; } available = true; notify(); return super.poll(); } }