ScheduledExecutorService的用法——定时执行两个任务
package control; 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 TimerManager {
//
Log log=Log.getLoger(); private ScheduledExecutorService scheduExec;
public long start;
public TimerManager() { this.scheduExec = Executors.newScheduledThreadPool(2); this.start = System.currentTimeMillis();
long oneDay = 24 * 60 * 60 * 1000;
long initDelayOne = getTimeMillis("3:00:00") - System.currentTimeMillis();
initDelayOne = initDelayOne > 0 ? initDelayOne : oneDay + initDelayOne;
long initDelayTwo = getTimeMillis("3:00:00") - System.currentTimeMillis();
initDelayTwo = initDelayTwo > 0 ? initDelayTwo : oneDay + initDelayTwo; } public void timerOne(){
scheduExec.scheduleWithFixedDelay(new Runnable() {
public void run() {
...
System.out.println("timerOne invoked ....."); }
},0,5000, TimeUnit.MILLISECONDS);
} public void timerTwo(){
scheduExec.scheduleAtFixedRate(new Runnable() {
public void run() {
...
System.out.println("timerTwo invoked .....");
}
},2000,5000,TimeUnit.MILLISECONDS); } /**
* @param time "HH:mm:ss"
* @return
*/
private static long getTimeMillis(String time) {
try {
DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
return curDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
} public static void main(String[] args) {
TimerManager test = new TimerManager();
test.timerOne();
}
}
ScheduledExecutorService的用法——定时执行两个任务的更多相关文章
- linux定时执行任务crontab命令用法
linux系统的定时任务是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另外, 由于使用者自己也可以设置计划任务,所 ...
- Oracle通过JOB定时执行存储过程实现两表数据比对
需求: 第三方云平台管理的虚拟机会进行关机.资源扩展等操作,因此开关机状态.CPU.内存.磁盘大小等数据需要进行同步.这里第三方云平台是BMC CLM云平台,底层虚拟化平台是Vcenter.进行同步的 ...
- 【转】linux 定时执行shell脚本
在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程,在像备份等需要在操作系统级定时任务只能采用crontab来完成 本文讲述crontab具体用法,以供备忘. 在oracle ...
- Linux下定时执行脚本(转自Decode360)
文章来自:http://www.blogjava.net/decode360/archive/2009/09/18/287743.html Decode360's Blog 老师(业精于勤而荒于嬉 ...
- 如何使用Linux的Crontab定时执行PHP脚本的方法
我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...
- Linux下的crontab定时执行任务命令详解
在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“cr ...
- crontab定时执行任务
第一部分 crontab介绍 每个操作系统都有它的自动定时启动程序的功能,Windows有它的任务计划,而Linux对应的功能是crontab. crontab简介 crontab命令常见于Unix和 ...
- 关于oracle dbms_job 定时执行的内容。
一.设置初始化参数 job_queue_processessql> alter system set job_queue_processes=n;(n>0)job_queue_proces ...
- linux 定时执行shell脚本
转自:http://blog.csdn.net/jingxiangren/article/details/4745631 在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程, ...
随机推荐
- python打印详细的异常信息
#!/usr/bin/env python #coding=utf-8 import traceback try: 1/0 except Exception, e: print e print tra ...
- Android(java)学习笔记79:java中InetAddress类概述和使用
要想让网络中的计算机能够互相通信,必须为每台计算机指定一个标识号,通过这个标识号来指定要接受数据的计算机和识别发送的计算机. 在TCP/IP协议中,这个标识号就是IP地址. 那么,我们如果获取和操作I ...
- angular 项目回顾
从学习angular,到实际项目开发不到一周,完全是边写边学呀,都是为了项目,已使用angular 开发了两个项目了,有些技术当时只是会用,都没好好回顾一下,现在有时间回顾一下,项目中用到的一些指令, ...
- oracle的控制语句if和循环语句loop while for
pl/sql的控制结构if-then if-then-else if-then-elsif-else 案例1:编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该雇员工资增加10% ...
- KindEditor图片上传到七牛云
自己做了一个网站,编辑器用的是KindEditor,平时会涉及到KindEditor自带的图片上传,但是服务器用的是虚拟主机,没多少空间,就一直想着把图片放在免费的云存储空间,之前看KindEdito ...
- Oracle中exists与in的区别
有两个简单例子,以说明 "exists"和"in"的效率问题 1) select * from T1 where exists(select 1 from T2 ...
- hdu 3093 动态规划
思路:直接引用论文的话. 暂时先不考虑“使剩下的物品都放不下”的条件,那就是求 0-1 背包的所有可行方案. 用 Fi[j]表示前 i 件物品中选若干件总体积为 j 的方案数,初始为 F0[0]=1, ...
- poj 3264 RMQ
直接写个RMQ就能过. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- PS基础学习 1---基本工具
1,选框工具: 选择以后对选框中的内容进行修改 ① Shift + 选框 为正方形 ② 选中后鼠标放在选框中对选择范围进行拖动 ③ 移动工具可以拉着选框中的内容移动 ④ ctrl+D取消选框 ⑤单行选 ...
- Nginx - HTTP Configuration, Module Variables
The HTTP Core module introduces a large set of variables that you can use within the value of direct ...