Scrap that last question. I made some mods to the code and I switched to using take() which blocks. Problem is that take() is blocking forever.Can anyone see what I am doing wrong?
Code:import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; import java.util.concurrent.DelayQueue; public class DelayTest{ public static void main(String[] args){ DelayQueue<DelayedElement> dq = new DelayQueue<DelayedElement>(); dq.put(new DelayedElement(10)); dq.put(new DelayedElement(20)); dq.put(new DelayedElement(5)); while(true){ try{ // DelayedElement de = dq.poll(); // if this queue has no elements with an unexpired delay no blocking DelayedElement de = dq.take(); // waits for an expired element then remove head of queue if(de == null) break; System.out.println(de.getDelayTime()); }catch(InterruptedException ie){ System.err.println(ie); } } } } class DelayedElement implements Delayed, Comparable<Delayed>{ private long delay; public long getDelay(TimeUnit unit){ long d = unit.convert(delay, TimeUnit.MILLISECONDS); return d; } public DelayedElement(long delay){ this.delay = delay; } public long getDelayTime(){ return delay; } public int compareTo(Delayed other){ if(other == this) return 0; DelayedElement x = (DelayedElement)other; long diff = delay - x.delay; if(diff < 0){ return -1; } return 1; // diff > 0 } }




Can anyone see what I am doing wrong?
Reply With Quote