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
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
- scheduleAtFixedRate与scheduleWithFixedDelay的区别
- scheduleAtFixedRate 与 scheduleWithFixedDelay 区别
- scheduleAtFixedRate与scheduleWithFixedDelay区别?
- scheduleWithFixedDelay 和 scheduleAtFixedRate 的区别
- java 定时任务scheduleAtFixedRate 与 scheduleWithFixedDelay区别
- schedule、scheduleAtFixedRate和scheduleWithFixedDelay的区别
- scheduleAtFixedRate和scheduleWithFixedDelay区别
- 关于scheduleWithFixedDelay与scheduleAtFixedRate的比较
- ScheduledExecutorService 的 scheduleAtFixedRate 和 scheduleWithFixedDelay 两个方法的区别
- ScheduledExecutorService的scheduleAtFixedRate和scheduleWithFixedDelay方法的区别
- JAVA定时器ScheduledExecutorService中,scheduleAtFixedRate和scheduleWithFixedDelay的区别
- ScheduledExecutorService 定时任务,scheduleAtFixedRate和scheduleWithFixedDelay区别
- (原创)定时线程池中scheduleWithFixedDelay和scheduleAtFixedRate的区别
- ScheduledExecutorService定时周期执行指定的任务 ,关于scheduleAtFixedRate 和 scheduleWithFixedDelay使用区别
- 黑马程序员_java 面向对象的理解
- Input.GetAxis();
- 我所理解的设计模式(C++实现)——装饰者模式(Decorator Pattern)
- 南阳oj 题目67 三角形面积
- [图像]获取图片中感兴趣区域的信息(Matlab实现)
- scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
- 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理已经中止。该值可能指示存在危及应用程序安全的尝试,如跨站点脚本攻击。若要允许页面重写应用程序请求验证设置
- 【XML】——DTD & Schema
- spark安装常见错误
- hdu 1789 Doing Homework again 贪心
- Delphi XE8,C++ Builder XE8,RadStudio XE8 Update1 架构师版(附破解激活)
- 黑马程序员_java 基本语句的理解
- Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/ma ven/cli/Maven
- 13_Android的生命周期