关于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线程的创建与销毁需要一定 ...
随机推荐
- em 与 rem 区别.
em 与自身 字体大小有关. rem 与 body 的字体大小有关..
- C++_类继承3-动态联编和静态联编
程序调用函数时,将使用哪个可执行代码块呢?编译器负责回答这个问题. 将源代码中的函数调用解释为特定的函数代码块被称为函数名联编(binding). 在C语言中,这非常简单,因为每个函数名对应一个不同的 ...
- [BZOJ 4850][Jsoi2016]灯塔
传送门 #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) ...
- .Net支持Redis哨兵模式
csredis 博客 csRedisgit地址 csRedis3.2.1 Nuget地址 (在使用csredis3.2.1获取sentinel时产生运行时异常,调查问题最后发现是获取sentinel的 ...
- lambda 表达式定制操作
泛型算法中的定制操作 许多算法都会比较输入序列中的元素以达到排序的效果,通过定制比较操作,可以控制算法按照编程者的意图工作. 普通排序算法: template<class RandomItera ...
- Putty使用帐号和密码的自动登录
Putty使用ssh key做验证登陆是最方便的,不用密码.如果不想做key exchange,只是单纯想保存帐号密码做自动登陆,可以借助bat文件的方式如下,其中MyServer是已经保存了的ses ...
- ndoejs创建多重文件夹
function mkdir(filepath){ var path=require("path") if(!fs.existsSync(path.dirname(filepath ...
- Notepad++编译和运行Java
首先要让Notepad++编译和运行Java,前提是电脑里已经配置好了Java的环境(这里可以参考我博客里关于Java环境配置的那篇随笔). 在Notepad++上面的选项栏中找到 插件---> ...
- 3.Exadata 软件体系结构
整体架构和 smart scan Aasm Ehcc (混合例压缩 和 存储索引) SCAN Service 和 server pool DB SERVER -> DB instance -&g ...
- Java调度线程池ScheduleExecutorService(续)
链接 Java线程池详解(一) Java线程池详解(二) Java调度线程池ScheduleExecutorService 上面列出了最近写的关于java线程池ScheduleExecutorServ ...