scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别

来源:互联网 发布:中国的网络发展历程 编辑:程序博客网 时间:2024/06/10 08:06
package thread;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledFuture;import java.util.concurrent.ThreadFactory;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicLong;public class Test03 {private static final AtomicLong l = new AtomicLong(0) ;private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);public static void main(String[] args) {/*  * 使用scheduleAtFixedRate , 该方法第三个参数表示在上一个个任务开始执行之后延迟 * 多少秒之后再执行, 是从上一个任务开始时开始计算 * 但是还是会等上一个任务执行完之后,下一个任务才开始执行,最后的结果,就是感觉延迟失去 * 了作用 *  */ScheduledFuture<?> sf1 = scheduler.scheduleAtFixedRate(new Runnable() {public void run() {long i =  l.getAndAdd(1) ;System.out.println("start " + i);try {TimeUnit.SECONDS.sleep(5) ;} catch (InterruptedException e) {  e.printStackTrace();}System.out.println("end " + i);}}, 0, 2 , TimeUnit.SECONDS) ;/*  * 使用scheduleWithFixedDelay该方法第三个参数表示在上一个个任务结束执行之后延迟 * 多少秒之后再执行, 是从上一个任务结束时开始计算 *  */ScheduledFuture<?> sf2 = scheduler.scheduleWithFixedDelay(new Runnable() {public void run() {long i =  l.getAndAdd(1) ;System.out.println("start " + i);try {TimeUnit.SECONDS.sleep(3) ;} catch (InterruptedException e) {  e.printStackTrace();}System.out.println("end " + i);}}, 0, 2, TimeUnit.SECONDS) ;}}
<span style="background-color: rgb(255, 255, 255);">//------------</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">                                                      </span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">自己看到这样子的结果也觉得比较奇怪,这个scheduleAtFixedRate()方法按照理解应该是直接每个2秒中执行一次。</span></span><pre name="code" class="java" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; background-color: rgb(245, 245, 245);"> </span><span style="box-sizing: content-box; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; color: rgb(255, 0, 0); background-color: rgb(245, 245, 245);">If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.</span></span>
<span style="font-family:Helvetica, Tahoma, Arial, sans-serif;color:#ff0000;"><span style="line-height: 24px; background-color: rgb(245, 245, 245);">这个函数的英文文档的一句话已经说明了,这个方法执行的时间可能会超过执行频率的周期,那么后面的任务会延期执行了。</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span>
0 0