首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >延迟队列延迟后的轮询

延迟队列延迟后的轮询
EN

Stack Overflow用户
提问于 2014-08-04 06:11:35
回答 1查看 1.6K关注 0票数 1

我有一个示例程序,我试图从其中了解延迟队列。我将person对象提供给队列中具有特定延迟的对象,当我尝试在间隔5秒后轮询对象时,应该得到延迟过期的所有对象。但是,相反,我得到了空,我不明白的原因。但是当我将延迟设置为0时,这个轮询就能工作了。有谁能帮我计算一下下面的示例代码中我在哪里出错?

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-04 06:36:28

getDelay实现是错误的

代码语言:javascript
复制
@Override
    public long getDelay(TimeUnit timeUnit) {       
        **// This will never return zero! and the element is never available.**
        return timeUnit.convert(this.runningTime, TimeUnit.MILLISECONDS);
    }

试着做这样的事

代码语言:javascript
复制
@Override
    public long getDelay(TimeUnit timeUnit) {           
        return timeUnit.convert(endOfDelay - System.currentTimeMillis(),
                          TimeUnit.MILLISECONDS);
    }

其中endOfDelay设置为长( System.currentTimeMillis() +延迟(以ms为单位))

下面是代码的工作部分:

代码语言:javascript
复制
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);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25112676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档