ScheduledExecutorService定时周期执行指定的任务
示例代码
package com.effective.common.concurrent.execute; import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; public class Schedule { private static DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
private static DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd"); private static ScheduledExecutorService excutor = Executors.newSingleThreadScheduledExecutor(); /**
* 按指定频率周期执行某个任务 <br>
* 初始化延迟0ms开始执行,每隔5ms重新执行一次任务。
*/
public void fixedRate(){
excutor.scheduleAtFixedRate(new EchoServer(), //执行线程
0, //初始化延迟
5000, //两次开始的执行的最小时间间隔
TimeUnit.MILLISECONDS //计时单位
);
} /**
*
*/
public void fixDelay(){
excutor.scheduleWithFixedDelay(new EchoServer(),//执行线程
0, //初始化延迟
5000, //前一次执行结束到下一次执行开始的间隔时间
TimeUnit.MILLISECONDS);
} /**
* 每天晚上8点执行一次
*/
public void dayOfDelay(String time){
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
long oneDay = 24 * 60 * 60 * 1000;
long initDelay = getTimeMillis("20:00:00") - System.currentTimeMillis();
initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;
executor.scheduleAtFixedRate(
new EchoServer(),
initDelay,
oneDay,
TimeUnit.MILLISECONDS);
} /**
* 获取给定时间对应的毫秒数
* @param string "HH:mm:ss"
* @return
*/
private static long getTimeMillis(String time) {
try {
Date currentDate = dateFormat.parse(dayFormat.format(new Date()) + " " +time);
return currentDate.getTime() ;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
} public static void main(String[] args){ Schedule schedule = new Schedule();
schedule.fixedRate();
schedule.fixDelay(); } }
ScheduledExecutorService定时周期执行指定的任务的更多相关文章
- ScheduledExecutorService定时周期运行指定的任务
一:简单说明 ScheduleExecutorService接口中有四个重要的方法,当中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比較方便. ...
- ScheduledThreadPoolExecutor源码分析-你知道定时线程池是如何实现延迟执行和周期执行的吗?
Java版本:8u261. 1 简介 ScheduledThreadPoolExecutor即定时线程池,是用来执行延迟任务或周期性任务的.相比于Timer的单线程,定时线程池在遇到任务抛出异常的时候 ...
- 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法
[源码下载] 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法 作者:webabcd 介绍重新想象 Wi ...
- Shell脚本实现每个工作日定时执行指定程序
我们可能会遇到这样的情景:必须在每个工作日定时执行Linux服务器上的某个程序.那么有没有办法实现这一功能呢?答案是肯定的.我们可以运用Shell脚本简单实现这一功能. 原理很简单,Shell脚本内部 ...
- Linux下的crontab定时、执行任务命令详解 oracle 自动备份
在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为&qu ...
- linux进程管理-定时定期执行任务
0.计划任务的命令: at 安排作业在某一时刻执行 batch 安排作业在系统负载不重时执行 crontab 安排周期性运行的作业 1.at命令用法: 安排命令或者多个命令在指定的时间运行一次 语法 ...
- ThreadPoolTimer -延迟执行, 周期执行
介绍重新想象 Windows 8 Store Apps 之 线程池 通过 ThreadPoolTimer 实现延迟执行 通过 ThreadPoolTimer 实现周期执行 通过 ThreadPool ...
- 转 Windows 7设置定时自动执行任务方法
在使用电脑的时候可能会遇到一些需要无人值守让电脑自行执行任务后定时关机的情形,在Win7系统中,我们可以使用"任务计划"设置功能结合 shutdown命令灵活设置任务计划,让Win ...
- Hadoop生态圈-Azkaban实战之Command类型执行指定脚本
Hadoop生态圈-Azkaban实战之Command类型执行指定脚本 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.服务端测试代码(别忘记添加权限哟!) [yinzh ...
随机推荐
- ECharts 在同一个页面添加多个图表 并 给图表绑定事件
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts< ...
- Mysql主从库同步错误:1062 Error 'Duplicate entry '1438019'
mysql主从库同步错误:1062 Error 'Duplicate entry '1438019' for key 'PRIMARY'' on query mysql主从库在同步时会发生1062 L ...
- CALayer 图层
// CALayer 图层属性,继承UIView都有该属性,可设置边框宽度.颜色.圆角.阴影等 UIImageView *imageView = [[UIImageView alloc]initWit ...
- fetch the words from url
python code: import time,urllib fid=open('Friedrich Nietzsche Classic Words.txt','w') #1st ss='http: ...
- 常用<meta>标签
页面关键词 <meta name="keywords" content="your tags" /> 页面描述 <meta name=&quo ...
- Dive into python 实例学python (1) —— 函数和测试
odbchelper.py def buildConnectionString(params): """Build a connection string from a ...
- Lintcode: Count of Smaller Number
Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...
- iOS 检查版本号的代码
- (void)checkNewVersion{ if ([@"appStore" isEqualToString:CHANNEL]) { AFHTTPRequestOperati ...
- Android中异步类AsyncTask的理解
这里有两种解释的方法,各有侧重点: 第一种解释: Async Task 简介:AsyncTask的特点是任务在主线程之外运行,而回调方法是在主线程中执行,这就有效地避免了使用Handler带来的麻烦 ...
- 9. 星际争霸之php设计模式--代理模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...