java getdelay_java中DelayQueue的一个使用陷阱分析

最近工作中有接触到DelayQueue,网上搜索资料的时候发现一篇文章谈到DelayQueue的坑。点击打开链接

文中已经总结了遇到坑的地方,还有解决方案。不过我第一眼看一下没弄明白为什么,所以翻了翻源码深究了一下,下面把这个坑的原因以及原理分析一下。

首先是DelayQueue的take()方法:

1 public E take() throwsInterruptedException {2 final ReentrantLock lock = this.lock;3 lock.lockInterruptibly();4 try{5 for(;;) {6 E first =q.peek();7 if (first == null)8 available.await();9 else{10 long delay = first.getDelay(NANOSECONDS); //1

11 if (delay <= 0)12 returnq.poll();13 first = null; //don't retain ref while waiting

14 if (leader != null)15 available.await();16 else{17 Thread thisThread =Thread.currentThread();18 leader =thisThread;19 try{20 available.awaitNanos(delay); //2

21 } finally{22 if (leader ==thisThread)23 leader = null;24 }25 }26 }27 }28 } finally{29 if (leader == null && q.peek() != null)30 available.signal();31 lock.unlock();32 }33 }

首先看到注释2,这是一个带时间的await方法,时间单位是纳秒,传入的参数delay是从注释1通过调用first对象的getDelay方法获取的。first对象是E类型的,E是一个实现了Delayed接口的泛型。

这里看看接口Delayed的源码:

1 public interface Delayed extends Comparable{2

3 /**

4 * Returns the remaining delay associated with this object, in the5 * given time unit.6 *7 *@paramunit the time unit8 *@returnthe remaining delay; zero or negative values indicate9 * that the delay has already elapsed10 */

11 longgetDelay(TimeUnit unit);12 }

就只有一个getDelay(TimeUnit)方法,它返回的指定的TimeUnit的时间长度。显然,具体的实现类要实现该方法才行。

那么来看一下具体的getDelay(TimeUnit)方法的实现吧,我看了几篇文章,基本上大同小异,都是如下这般实现的:

1 public longgetDelay(TimeUnit unit) {2 return unit.convert(this.expire -System.currentTimeMillis() , TimeUnit.MILLISECONDS);3 }

原博主很贴心的提醒了,这个地方convert方法的第二个参数,应该要使用TimeUnit.MILLISECONDS而不是TimeUnit.NANOSECONDS(虽然不管使用什么时间单位都不会导致程序出现错误的结果,但是用错了时间单位的话,CPU可就遭殃了)。那么为什么会一定要强调要使用MILLISECONDS这个单位呢?

继续看看convert方法的源码吧,在TimeUnit枚举类中,定义了若干时间单位,他们有各自的convert方法的实现,先来看看TimeUnit.NANOSECONDS的:

1 NANOSECONDS {2 public long toNanos(long d) { returnd; }3 public long toMicros(long d) { return d/(C1/C0); }4 public long toMillis(long d) { return d/(C2/C0); }5 public long toSeconds(long d) { return d/(C3/C0); }6 public long toMinutes(long d) { return d/(C4/C0); }7 public long toHours(long d) { return d/(C5/C0); }8 public long toDays(long d) { return d/(C6/C0); }9 public long convert(long d, TimeUnit u) { returnu.toNanos(d); }10 int excessNanos(long d, long m) { return (int)(d - (m*C2)); }11 },

可以看到,convert方法又直接调用了TimeUnit.toNanos方法,直接就把第一个参数d当做一个纳秒的时间长度给返回了。

同理看看TimeUnit.MILLISECONDS定义的方法:

1 MILLISECONDS {2 public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); } //static final long C0 = 1L; static final long C1 = C0 * 1000L;static final long C2 = C1 * 1000L;

3 public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); }4 public long toMillis(long d) { returnd; }5 public long toSeconds(long d) { return d/(C3/C2); }6 public long toMinutes(long d) { return d/(C4/C2); }7 public long toHours(long d) { return d/(C5/C2); }8 public long toDays(long d) { return d/(C6/C2); }9 public long convert(long d, TimeUnit u) { returnu.toMillis(d); }10 int excessNanos(long d, long m) { return 0; }11 },

回到我们的实际使用场景,take方法中long delay = first.getDelay(NANOSECONDS);  ->  NANOSECONDS.convert(long d, TimeUnit u)  ->  u.toNanos(d)。如果我们在getDelay方法实现中,convert方法第二个参数传入的是NANOSECONDS,那么就直接返回d;如果convert方法第二个参数传入的是MILLISECONDS,那么返回就是MILLISECONDS.toNanos(d),得到的结果就是1000*1000*d。

可以发现,convert方法的第二个参数TimeUnit,实际上是跟着第一个参数d的时间单位走的。如果实现时候直接使用time - System.currentTimeMillis()作为第一个参数,实际上它的时间单位确实应该是MILLISECONDS,那么如果第二个参数传错了为NANOSECONDS,那就导致take方法中的awaitNanos方法等待时间缩短了1000*1000倍,这样带来的cpu空转压力是巨大的。

分析了这么多,其实看看jdk中TimeUnit类对convert方法的注释,很容易就理解了:

/**

* Converts the given time duration in the given unit to this unit.

* Conversions from finer to coarser granularities truncate, so

* lose precision. For example, converting {@code 999} milliseconds

* to seconds results in {@code 0}. Conversions from coarser to

* finer granularities with arguments that would numerically

* overflow saturate to {@code Long.MIN_VALUE} if negative or

* {@code Long.MAX_VALUE} if positive.

*

*

For example, to convert 10 minutes to milliseconds, use:

* {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}

*

* @param sourceDuration the time duration in the given {@code sourceUnit}

* @param sourceUnit the unit of the {@code sourceDuration} argument

* @return the converted duration in this unit,

* or {@code Long.MIN_VALUE} if conversion would negatively

* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.

*/

public long convert(long sourceDuration, TimeUnit sourceUnit) {

throw new AbstractMethodError();

}

这里很明确的指出了,convert方法的第二个参数sourceUnit(@param sourceUnit the unit of the {@code sourceDuration} argument)应该是第一个参数sourceDuration的时间单位。会产生原链接中提到的那样的错误使用,应该就是理解错了这个convert方法参数的含义,以为第二个参数的时间单位是要转换到的时间单位。

不过这个陷阱确实有点绕,在getDelay(TimeUnit unit)方法里面,调用unit.convert(long sourceDuration, TimeUnit sourceUnit)方法,一下出来了两个TimeUnit变量,不仔细一点的话真是容易被坑啊。当然,要是自身的getDelay方法实现不用unit.convert方法或许就避免了该问题了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaDelayQueue一个基于优先级队列实现的延迟队列。它可以用于定时任务调度、缓存过期等场景。 DelayQueue的元素必须实现Delayed接口,该接口继承自Comparable接口,因此元素需要实现compareTo方法,以便在队列维护元素的优先级。 DelayQueue的元素按照延迟时间的大小进行排序,即延迟时间短的元素排在队列的前面。当从队列取出元素时,只有延迟时间到了的元素才会被取出。 以下是一个使用DelayQueue的简单示例: ```java import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; public class DelayQueueDemo { public static void main(String[] args) throws InterruptedException { DelayQueue<DelayedElement> queue = new DelayQueue<>(); queue.add(new DelayedElement("task1", 3000)); // 延迟3秒执行 queue.add(new DelayedElement("task2", 2000)); // 延迟2秒执行 queue.add(new DelayedElement("task3", 1000)); // 延迟1秒执行 while (!queue.isEmpty()) { DelayedElement element = queue.take(); // 取出元素 System.out.println(System.currentTimeMillis() + ": " + element); } } } class DelayedElement implements Delayed { private String name; private long expireTime; public DelayedElement(String name, long delay) { this.name = name; this.expireTime = System.currentTimeMillis() + delay; } @Override public long getDelay(TimeUnit unit) { return unit.convert(expireTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } @Override public int compareTo(Delayed o) { return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS)); } @Override public String toString() { return "DelayedElement{" + "name='" + name + '\'' + ", expireTime=" + expireTime + '}'; } } ``` 在上面的示例,我们创建了一个DelayQueue对象,并向其添加了三个DelayedElement元素,分别表示3秒、2秒和1秒后执行的任务。然后在一个循环不断取出元素,直到队列为空。由于每个元素的延迟时间不同,因此取出的顺序也是不同的。 以上就是JavaDelayQueue的简单使用方法。需要注意的是,DelayQueue是线程安全的,可以在多线程环境下使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值