首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置延迟队列的延迟

如何设置延迟队列的延迟
EN

Stack Overflow用户
提问于 2014-04-22 12:36:47
回答 3查看 7.8K关注 0票数 4

我刚开始用java编写代码,我正在努力设置一个DelayQueue,

我想这么做,

代码语言:javascript
复制
DelayQueue queue = new DelayQueue();

If (counter > 0){
queue.offer(Integer, *A custom delay*)
} Else {
queue.offer(Integer, *A different custom delay*)
}

我只是试着学习所有的基础知识,我已经阅读了API,似乎无法理解它。

提前感谢

EN

回答 3

Stack Overflow用户

发布于 2016-12-29 13:41:12

Delayed的这种实现是很好的,因为:

  • compareTo()的实现不执行任何类的转换,消除了抛出ClassCastException的可能性。
  • compareTo()的实现在转换为int之前使用Math.minMath.max函数,以适当防止溢出错误
  • getDelay()的实现正确地转换单元并实际返回剩余时间

TestDelay类实现Delayed

代码语言:javascript
复制
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class TestDelay implements Delayed
{
    public final Long delayMillis;
    public final Long expireTimeMillis;

    public TestDelay(Long delayMillis)
    {
        this.delayMillis = delayMillis;
        this.expireTimeMillis = System.currentTimeMillis()+delayMillis;
    }

    @Override
    public final int compareTo(@NotNull Delayed o)
    {
        long diffMillis = getDelay(TimeUnit.MILLISECONDS)-o.getDelay(TimeUnit.MILLISECONDS);
        diffMillis = Math.min(diffMillis,1);
        diffMillis = Math.max(diffMillis,-1);
        return (int) diffMillis;
    }

    @Override
    public final long getDelay(@NotNull TimeUnit unit)
    {
        long delayMillis = expireTimeMillis-System.currentTimeMillis();
        return unit.convert(delayMillis,TimeUnit.MILLISECONDS);
    }
}

JUnit单元测试展示了使用TestDelay类的示例:

代码语言:javascript
复制
import org.junit.Test;

import java.util.concurrent.DelayQueue;

public class DelayQueueTest
{
    @Test
    public final void generalTest() throws InterruptedException
    {
        DelayQueue<TestDelay> q = new DelayQueue<>();
        q.put(new TestDelay(500L));
        q.put(new TestDelay(2000L));
        q.put(new TestDelay(1000L));
        q.put(new TestDelay(10L));
        q.put(new TestDelay(3000L));
        while (!q.isEmpty())
        {
            System.out.println(q.take().delayMillis);
        }
    }
}

DelayQueueTest输出

票数 8
EN

Stack Overflow用户

发布于 2015-08-03 10:33:30

代码语言:javascript
复制
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayQueueExample {

    public static void main(String[] args) {

        BlockingQueue<DelayedElement> blockingQueue = new DelayQueue<DelayedElement>();

        try {
            blockingQueue
                    .put(new DelayedElement(4000, "Message with delay 4s"));
            blockingQueue
                    .put(new DelayedElement(2000, "Message with delay 2s"));
            blockingQueue
                    .put(new DelayedElement(9000, "Message with delay 9s"));
        } catch (InterruptedException ie) {
        }

        while (!blockingQueue.isEmpty()) {
            try {
                System.out.println(">>" + blockingQueue.take());
            } catch (InterruptedException ie) {
            }

        }

    }
}

class DelayedElement implements Delayed {

    private long duration = 0;
    private String message;

    public DelayedElement(long duration, String name) {
        this.duration = System.currentTimeMillis() + duration;
        this.message = name;
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.duration - ((DelayedElement) o).getDuration());
    }

    @Override
    /*
     * Expiration occurs when an element's getDelay(TimeUnit unit) method
     * returns a value less than or equal to zero.
     */
    public long getDelay(TimeUnit unit) {
        long diff = duration - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "DelayedElement [duration=" + duration + ", message=" + message
                + "]";
    }

}
票数 7
EN

Stack Overflow用户

发布于 2014-04-22 12:44:37

您的“自定义延迟”类必须从getDelay(TimeUnit timeUnit)接口中指定的Delayed方法返回延迟。例如。

代码语言:javascript
复制
public class MyClass implements Delayed {
    public long getDelay(TimeUnit timeUnit) {
        long delay = calculateDelaySomehow();
        return delay;
    }
}

注意,您还需要为compareTo()提供一个实现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23219801

复制
相关文章

相似问题

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