import java.util.concurrent.*;
import java.util.*;
class DelayObject implements Delayed {
private String name;
private long time;
public DelayObject(String name, long delayTime)
{
this.name = name;
this.time = System.currentTimeMillis()
+ delayTime;
}
@Override
public long getDelay(TimeUnit unit)
{
long diff = time - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
@Override
public int compareTo(Delayed obj)
{
if (this.time < ((DelayObject)obj).time) {
return -1;
}
if (this.time > ((DelayObject)obj).time) {
return 1;
}
return 0;
}
@Override
public String toString()
{
return "\n{"
+ " " + name + ", time=" + time + "}";
}
}
public class GFG {
public static void main(String[] args) throws InterruptedException
{
BlockingQueue<DelayObject> DQ
= new DelayQueue<DelayObject>();
DQ.add(new DelayObject("A", 1));
DQ.add(new DelayObject("B", 2));
DQ.add(new DelayObject("C", 3));
DQ.add(new DelayObject("D", 4));
Iterator val = DQ.iterator();
System.out.println("The iterator values are: ");
while (val.hasNext()) {
System.out.println(val.next());
}
}
}