我刚开始用java编写代码,我正在努力设置一个DelayQueue,
我想这么做,
DelayQueue queue = new DelayQueue();
If (counter > 0){
queue.offer(Integer, *A custom delay*)
} Else {
queue.offer(Integer, *A different custom delay*)
}我只是试着学习所有的基础知识,我已经阅读了API,似乎无法理解它。
提前感谢
发布于 2016-12-29 13:41:12
Delayed的这种实现是很好的,因为:
compareTo()的实现不执行任何类的转换,消除了抛出ClassCastException的可能性。compareTo()的实现在转换为int之前使用Math.min和Math.max函数,以适当防止溢出错误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输出

发布于 2015-08-03 10:33:30
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
+ "]";
}
}发布于 2014-04-22 12:44:37
您的“自定义延迟”类必须从getDelay(TimeUnit timeUnit)接口中指定的Delayed方法返回延迟。例如。
public class MyClass implements Delayed {
public long getDelay(TimeUnit timeUnit) {
long delay = calculateDelaySomehow();
return delay;
}
}注意,您还需要为compareTo()提供一个实现。
https://stackoverflow.com/questions/23219801
复制相似问题