在延迟队列中延迟后进行轮询



我有一个示例程序,从中我试图理解延迟队列。我以特定的延迟向队列提供人员对象,当我尝试在间隔5秒后轮询对象时,我应该获得延迟已过期的所有对象。但是我得到的是空,我不明白为什么。但是当我将延迟设置为0时,此轮询工作。有人能帮我弄清楚我在哪里犯错误的示例代码如下?

public class DelayedQueue {
    public static void main(String[] args) {
        BlockingQueue<Person> queue = new DelayQueue<Person>();
        Person a = new Person("ram", "chennai", 1);
        Person b = new Person("nick", "manali", 1);
        Person c = new Person("sam", "delhi", 2);
        try {
            queue.offer(a);
            queue.offer(b);
            queue.offer(c);
            System.out.println(queue.poll(5, TimeUnit.SECONDS));
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
class Person implements Delayed {
    private String name;
    private String place;
    private int runningTime;
    public Person(String name, String place, int runningTime) {
        this.name = name;
        this.place = place;
        this.runningTime = runningTime;
    }
    public long getDelay(TimeUnit timeUnit) {           
        return timeUnit.convert(this.runningTime, TimeUnit.MILLISECONDS);
    @Override
    public int compareTo(Delayed person) {
        Person b = (Person)person;   
        return this.name.compareTo(b.name);
    }
    @Override
    public long getDelay(TimeUnit timeUnit) {           
        return timeUnit.convert(this.runningTime, TimeUnit.MILLISECONDS);
    }
}

你的getDelay实现是错误的

@Override
    public long getDelay(TimeUnit timeUnit) {       
        **// This will never return zero! and the element is never available.**
        return timeUnit.convert(this.runningTime, TimeUnit.MILLISECONDS);
    }

试着这样做

@Override
    public long getDelay(TimeUnit timeUnit) {           
        return timeUnit.convert(endOfDelay - System.currentTimeMillis(),
                          TimeUnit.MILLISECONDS);
    }

endOfDelay设置为long (System.currentTimeMillis() + delay (ms)

下面是你的工作代码:

public class DelayedQueue
{
    public static void main(String[] args)
    {
        BlockingQueue<Person> queue = new DelayQueue<Person>();
        Person a = new Person("ram", "chennai", 1);
        Person b = new Person("nick", "manali", 1);
        Person c = new Person("sam", "delhi", 2);
        try
        {
            queue.offer(a);
            queue.offer(b);
            queue.offer(c);
            System.out.println(queue.poll(2, TimeUnit.SECONDS));
        } catch (InterruptedException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
class Person implements Delayed
{
    private String name;
    private String place;
    private long delayTime;
    public Person(String name, String place, long delayTime)
    {
        this.name = name;
        this.place = place;
        this.delayTime = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(delayTime);
    }
    @Override
    public int compareTo(Delayed person)
    {
        Person b = (Person) person;
        return this.name.compareTo(b.name);
    }
    @Override
    public long getDelay(TimeUnit timeUnit)
    {
        return timeUnit.convert(delayTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }
}

相关内容

  • 没有找到相关文章

最新更新