关于scheduleAtFixedRate方法与scheduleWithFixedDelay的使用
一、scheduleAtFixedRate方法
该方法是ScheduledExecutorService中的方法,用来实现周期性执行给定的任务,public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);
command:是给定的任务
initial:表示第一次开始任务时要延迟的时间
period:表示每次执行此任务要间隔的时间
unit: 是用来指定前两个的时间单位
执行次方后,开始计时,在延时结束后开始任务,并开始计算周期,下面分两种情况介绍
情况一:
执行任务的时间短,而周期长:这是执行完任务后,会等待周期结束,然后再次开始任务
情况二:
执行任务的时间长,而周期短;即在任务还未完成时,周期就已经结束,这时不能立马再开始一个任务,需要等待任务的完成,然后在开始任务,对于这种情况给出运行代码:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; public class MyscheduleAtFixedRate { public static void main(String[] args) {
// TODO 自动生成的方法存根
ScheduledExecutorService executor=Executors.newScheduledThreadPool(2);
System.out.println("X "+System.currentTimeMillis());
executor.scheduleAtFixedRate(new MyRunableA_atfix(), 1, 1, TimeUnit.SECONDS);
System.out.println("Y "+System.currentTimeMillis());
} }
class MyRunableA_atfix implements Runnable{ @Override
public void run() {
// TODO 自动生成的方法存根 System.out.println("begin A "+System.currentTimeMillis());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("end A "+System.currentTimeMillis());
}
}
运行结果:
X 1493197014282
Y 1493197014297
begin A 1493197015311
end A 1493197017340
begin A 1493197017340
end A 1493197019354
begin A 1493197019354
这表明了任务一开始,延时1S,然后在周期结束后,等待当前任务的完成,在进行循环任务
二、scheduleAtFixedRate方法
此方法也是循环执行任务,这个方法的参数意义与scheduleAtFixedRate方法的相同
此方法设置的周期,开始计时的时间与上个方法不同,它是在任务完成之后开始计时的,表示的是上一个任务与下一个任务之间的时间间隔
关于scheduleAtFixedRate方法与scheduleWithFixedDelay的使用的更多相关文章
- ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别
ScheduledExecutorService中scheduleAtFixedRate方法与scheduleWithFixedDelay方法的区别 ScheduledThreadPoolExecut ...
- ScheduledThreadPoolExecutor的scheduleAtFixedRate方法探究
ScheduledThreadPoolExecutor除了具有ThreadPoolExecutor的所有功能外,还可以延迟执行任务或者周期性的执 行某个任务.scheduleWithFixedDela ...
- 简单理解java中timer的schedule和scheduleAtFixedRate方法的区别
timer的schedule和scheduleAtFixedRate方法一般情况下是没什么区别的,只在某个情况出现时会有区别--当前任务没有来得及完成下次任务又交到手上. 我们来举个例子: 暑假到了老 ...
- Timer的schedule和scheduleAtFixedRate方法的区别解析(转)
在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...
- Timer的schedule和scheduleAtFixedRate方法的区别解析
在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下 (1)sched ...
- 20145325张梓靖 《Java程序设计》第6周学习总结
20145325张梓靖 <Java程序设计>第6周学习总结 教材学习内容总结 串流设计 输入串流(将数据从来源取出),代表对象为java.io.InputStream实例,输出串流(将数据 ...
- 第十章 Executor框架
在Java中,使用线程来异步执行任务.Java线程的创建与销毁需要一定的开销,如果我们为每一个任务创建一个新线程来执行,这些线程的创建与销毁将消耗大量的计算资源.同时,为每一个任务创建一个新线程来执行 ...
- 多线程编程学习十一(ThreadPoolExecutor 详解).
一.ThreadPoolExecutor 参数说明 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keep ...
- (转)深入详解Java线程池——Executor框架
转:https://yq.aliyun.com/articles/633782?utm_content=m_1000015330 在Java中,使用线程来异步执行任务.Java线程的创建与销毁需要一定 ...
随机推荐
- Domoticz 中添加彩云天气
前言 用过一段时间的彩云天气 APP,最吸引我的地方是精确到局部区域的天气预测,虽然准确度并不算高,但是对于预测下雨还是不错的选择.在 Domoticz 中添加彩云天气的数据,利用的是彩云天气提供的 ...
- python学习之路---day12
生成器和生成器表达式一:生成器 生成器实质上就是迭代器. 三种方式获取生成器: 01:通过生成器函数 02:通过各种推导式实现生成器 03:通过数据的转换也可以获取生成器 eg:普通函数 def fu ...
- IntelliJ IDEA(Android Studio)设置代码的快捷编辑模板Live Templates
1.file---->setttings 2.editor--->live template 3.点击右侧的+ 4.设置模板 注意:Abbreviation为代码模板的缩写.
- xml转换csv
/// <summary> /// xml文件转换为CSV /// </summary> /// <param name="fileName"> ...
- A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 ...
- HDU - 3033 滚动数组有坑
每层至少一个,滚动时要判上一层非法与否,所以每次都要memset #include<bits/stdc++.h> #define rep(i,j,k) for(int i=j;i<= ...
- table加载慢
为了让大表格(table)在下载的时候可以分段的显示,就是说在浏览器解析html时,table是作为一个整体解释的,使用tbody可以优化显示,如果表格很长,用tbody分段,可以一部分一部分地显示, ...
- [转] NOI, NOIP, IOI, ACM
[From] http://blog.csdn.net/chenbean/article/details/38928243 NOI是教育部和中国科协委托中国计算机学会举办了全国青少年计算机程序设计竞赛 ...
- Exception 'ReflectionException' with message 'Class require does not exist'
记录一下今天遇到的错误 在使用 <?= $form->field($model, 'content')->textarea() ?> 的时候报错 Exception 'Ref ...
- java se系列(一)开发前奏
1. 软硬件知识 电子计算机:俗称电脑,是一种能够按照程序运行,自动.高速处理海量数据的现代化智能电子设备.由硬件和软件所组成,没有安装任何软件的计算机称为裸机 cpu:是一台计算机的运算核心和控制核 ...