在延迟队列延迟后轮询

时间:2014-08-04 06:11:35

标签: java polling blockingqueue

我有一个示例程序,我试图了解延迟队列。我以特定的延迟向队列提供人物对象,当我在5秒的间隔后尝试轮询对象时,我应该得到延迟到期的所有对象。但相反,我得到null,因为我不明白其中的原因。但是,当我将延迟设置为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);
    }
}

1 个答案:

答案 0 :(得分:2)

您的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);
    }
}