package com.logicbig.example.delayqueue;
import java.time.LocalTime;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class DelayQueueExample {
public static void main(String... args) throws InterruptedException {
DelayQueue<Task> dq = new DelayQueue<>();
dq.put(new Task(1));
dq.put(new Task(2));
dq.put(new Task(3));
for (int i = 0; i < 3; i++) {
long l = System.currentTimeMillis();
Task task = dq.take();
System.out.println(task);
System.out.println("blocked for millis: " + (System.currentTimeMillis()-l));
System.out.println("----");
}
}
private static class Task implements Delayed {
private final long end;
private final int expectedDelay;
private int id;
Task(int id) {
this.id = id;
expectedDelay = ThreadLocalRandom.current().nextInt(100, 1000);
end = System.currentTimeMillis() + expectedDelay;
}
@Override
public long getDelay(TimeUnit unit) {
return unit.convert(end - System.currentTimeMillis(),
TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed o) {
return Long.compare(this.getDelay(TimeUnit.MILLISECONDS),
o.getDelay(TimeUnit.MILLISECONDS));
}
@Override
public String toString() {
return "Task{" +
"expectedDelay=" + expectedDelay +
", id=" + id +
'}';
}
}
}
Output
Task{expectedDelay=244, id=1}
blocked for millis: 259
----
Task{expectedDelay=948, id=3}
blocked for millis: 686
----
Task{expectedDelay=993, id=2}
blocked for millis: 45
----
package com.logicbig.example.delayqueue;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class DelayQueueExample2 {
public static void main(String... args) throws InterruptedException {
List<Task> list = Arrays.asList(new Task(1), new Task(2),
new Task(3));
DelayQueue<Task> dq = new DelayQueue<>(list);
for (int i = 0; i < 3; i++) {
long l = System.currentTimeMillis();
Task task = dq.take();
System.out.println(task);
System.out.println("blocked for millis: " + (System.currentTimeMillis() - l));
System.out.println("----");
}
}
private static class Task implements Delayed {
private final long end;
private final int expectedDelay;
private int id;
Task(int id) {
this.id = id;
expectedDelay = ThreadLocalRandom.current().nextInt(100, 1000);
end = System.currentTimeMillis() + expectedDelay;
}
@Override
public long getDelay(TimeUnit unit) {
return unit.convert(end - System.currentTimeMillis(),
TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed o) {
return Long.compare(this.getDelay(TimeUnit.MILLISECONDS),
o.getDelay(TimeUnit.MILLISECONDS));
}
@Override
public String toString() {
return "Task{" +
"expectedDelay=" + expectedDelay +
", id=" + id +
'}';
}
}
}
Output
Task{expectedDelay=694, id=1}
blocked for millis: 711
----
Task{expectedDelay=920, id=3}
blocked for millis: 203
----
Task{expectedDelay=997, id=2}
blocked for millis: 77
----