ScheduledExecutorService has 2 method to schedule a task
- schedule with fixed delay
- schedule at fixed rate
I was confused about delay and rate, they both require a time parameter. What's the difference?
I ran following code:
public static void main(String[] args) { ScheduledExecutorService executors = Executors.newSingleThreadScheduledExecutor(); executors.scheduleWithFixedDelay(new Runnable() { @Override public void run() { System.out.println(new Date()); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }, 0, 5, TimeUnit.SECONDS); }
And I got such a result on console. It will print a log every 10 seconds.
Wed Dec 03 23:13:44 CST 2014 Wed Dec 03 23:13:54 CST 2014 Wed Dec 03 23:14:04 CST 2014
I ran same code with the other method, this time I got:
Wed Dec 03 23:26:48 CST 2014 Wed Dec 03 23:26:53 CST 2014 Wed Dec 03 23:26:58 CST 2014
- "schedule with fixed delay" would delay given time since last task finished to start next execution
- "schedule at fixed rate" would wait given time since last task start to start next execution, if duration exceed given time, next task will be executed immediately.
相关推荐
定时器小例子,这是一个经较好定时器应用
这是一个jar,很秀。自定义的一个Java定时器工具类。主要是结合Spring Boot一起使用,并在Spring Boot启动的时候一起启动运行。
主要介绍了ScheduledExecutorService任务定时代码示例,具有一定借鉴价值,需要的朋友可以参考下
ScheduledExecutorService 计时器任务处理,用于超时处理。
您可以在工作线程上运行任务(Runnable或Callable),以指定执行计划(如ScheduledExecutorService),并指定执行时间。 介面 ScheduledFuture<?> schedule( Runnable command, int executeTime, long delay,...
下面小编就为大家带来一篇基于ScheduledExecutorService的两种方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
调度可以用 Timer 【调用 schedule() 或者 scheduleAtFixedRate() 方法实现】或者 ScheduledExecutorService 【结合工作中其它的需求,笔者选用此】 ScheduledExecutorService的初始化(线程池):
Springmvc java注解设置定时任务实例,利用 ScheduledExecutorService 接口实现
串行执行器服务 ScheduledExecutorService 的测试实用程序实现 允许测试如下代码: ... service.scheduleAtFixedRate(new Runnable() { @Override public void run() { count++; bar.doWhatever(); } },
Java通用任务执行程序,基于java.util.concurrent.ScheduledExecutorService。 Java通用的任务执行器,基于java.util.concurrent.ScheduledExecutorService工具类实现。 Maven: <groupId>...
springMVC配置、注解、列子
scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 1,2, TimeUnit.SECONDS); super.onStart(); } @Override protected void onStop() { //当Activity不可见的时候停止切换 ...
1.普通thread实现 2.TimerTask实现 3.ScheduledExecutorService实现
线程池执行者 ThreadPoolExecutor,定时执行者服务 ScheduledExecutorService, 使用 ForkJoinPool 进行分叉和合并,锁 Lock,读写锁 ReadWriteLock 原子性长整型 AtomicLong,原子性引用型 AtomicReference 修改数据:...
Java多线程入阶干货分享 1.使用线程的经验:设置名称、响应中断、使用ThreadLocal 2.Executor:ExecutorService和...8.定时器:ScheduledExecutorService、大规模定时器TimerWheel 9.并发三大定律 10.图书、相关网络资源
Android线程池管理的代码例子。用于演示普通线程池ThreadPoolExecutor、定时器线程池ScheduledExecutorService等功能。
1、使用线程的经验:设置...8、定时器: ScheduledExecutorService、大规模定时器TimerWheel 9、并发三大定律:Amdahl、Gustafson、Sun-Ni 10、神人和图书、相关网络资源 11、业界发展情况: GPGPU、OpenCL 12、复习题
内容列表 1、使用线程的经验:设置名称、...8、定时器: ScheduledExecutorService、大规模定时器TimerWheel 9、并发三大定律:Amdahl、Gustafson、Sun-Ni 10、神人和图书 11、业界发展情况: GPGPU、OpenCL 12、复习题
1、使用线程的经验:设置名称、响应中断、...8、定时器: ScheduledExecutorService、大规模定时器TimerWheel 9、并发三大定律:Amdahl、Gustafson、Sun-Ni 10、神人和图书 11、业界发展情况: GPGPU、OpenCL 12、复习题