4

我刚刚开始在 java 中编码我正在努力设置延迟队列,

我想拥有它,

DelayQueue queue = new DelayQueue();

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

我只是想学习所有的基础知识,并且我阅读了 API 并且似乎无法掌握它。

提前致谢

4

4 回答 4

8

这个实现Delayed很好,因为:

  • 的实现compareTo()不做任何类转换,消除了抛出一个的可能性ClassCastException
  • 在转换为之前执行compareTo()用途Math.min和功能,以正确防止溢出错误Math.maxint
  • 的实现getDelay()正确转换单位并实际返回剩余时间

TestDelay类实现Delayed

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该类的示例:

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

单元测试输出

于 2016-12-29T13:41:12.380 回答
7
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
                + "]";
    }

}
于 2015-08-03T10:33:30.603 回答
1

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

public class MyClass implements Delayed {
    public long getDelay(TimeUnit timeUnit) {
        long delay = calculateDelaySomehow();
        return delay;
    }
}

请注意,您还需要为compareTo().

于 2014-04-22T12:44:37.227 回答
0

DelayQueue元素保留在内部,直到某个延迟到期。元素必须实现接口java.util.concurrent.Delayed

例如,我创建了一个DelayedTest扩展延迟接口的类。这将实现 compareTo 和 getDelay() 方法

public class A{
    public static void main(String... args){
        DelayQueue  dq=new DelayQueue();
        DeleyedTest ob1=new DeleyedTest(10);
        DeleyedTest ob2=new DeleyedTest(5);
        DeleyedTest ob3=new DeleyedTest(15);

        dq.offer(ob1);
        dq.offer(ob2);
        dq.offer(ob3);

        Iterator itr=dq.iterator();
        while(itr.hasNext()){
            DeleyedTest dt=(DeleyedTest)itr.next();
            System.out.println(dt.deleyTime);
        }
    }
}
class DeleyedTest implements Delayed{
    public long deleyTime=0;
    DeleyedTest(long deleyTime){
        this.deleyTime=deleyTime;
    }

    @Override
    public int compareTo(Delayed ob) {
        if(this.deleyTime<((DeleyedTest)ob).deleyTime){
            return -1;
        }else if(this.deleyTime>((DeleyedTest)ob).deleyTime){
            return 1;
        }
        return 0;
    }
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(deleyTime-System.currentTimeMillis(),TimeUnit.NANOSECONDS); 
    }

}

结果:

5
10
15
于 2014-04-22T12:49:31.730 回答