I have the following supposedly trivially simple demo of a DelayQueue.
class DelayedThing implements Delayed {
private final long waitUntil;
private final String name;
public DelayedThing(String name, long wait) {
this.name = name;
this.waitUntil = System.currentTimeMillis() + wait;
System.out.println("DelayedThing(" + name + " wait=" + wait + " until-" + waitUntil);
}
@Override
public long getDelay(TimeUnit unit) {
System.out.println(name + " getDelay = " + unit.convert(waitUntil - System.currentTimeMillis(), TimeUnit.MILLISECONDS));
return unit.convert(waitUntil - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed o) {
long diff = this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS);
System.out.println(name + ".compareTo(" + o + ") = " + diff);
return Long.signum(diff);
}
@Override
public String toString() {
return name;
}
}
public void test() throws InterruptedException {
BlockingQueue<Delayed> queue = new DelayQueue<>();
queue.add(new DelayedThing("one second", 1000));
queue.add(new DelayedThing("two seconds", 2000));
queue.add(new DelayedThing("half second", 500));
for (Delayed d : queue) {
System.out.println(d);
}
}
but it prints
half second
two seconds
one second
which is obviously wrong.