timer.schedule的delay=0在api中没说,看源码发现是system.currentTimeMillis+delay作为执行时间,编码中发现好像有task未执行的情况,估计是delay太小(甚至为0),该执行时时间已经过了,就没执行了,看源码貌似是这样的,但不十分肯定,请问有谁遇到过同样的现象吗?大家来讨论下~~
PS:delay太大又没意义了,要的就是立即执行啊,至少延迟不能太大。

解决方案 »

  1.   

    看api的说明
     * <p>Corresponding to each <tt>Timer</tt> object is a single background
     * thread that is used to execute all of the timer's tasks, sequentially.
     * Timer tasks should complete quickly.  If a timer task takes excessive time
     * to complete, it "hogs" the timer's task execution thread.  This can, in
     * turn, delay the execution of subsequent tasks, which may "bunch up" and
     * execute in rapid succession when (and if) the offending task finally
     * completes.
     *
    Timer使用单线程来跑所有定时任务,按顺序地。如果有一个任务用了太久的执行时间,则它会“hog”住整个执行线程。这将会延迟后续的任务执行;
    那么对于一次性的任务,他们虽然会被前面的任务拖延时间,但是终归是要执行一次的。
    对于定时的循环的任务,则会给人一种丢失了前一(几)轮运行的感觉。
      

  2.   


    嗯,不过我说的不是定时任务,就是执行一次的任务,delay≈0
      

  3.   

    之所以要delay,是因为一些任务,初始化时,需要一些资源准备的时间。如果任务本身申请资源速度快到可以忽略不计,就可以不设置delay的。