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的用法——定时执行两个任务的更多相关文章

  1. linux定时执行任务crontab命令用法

    linux系统的定时任务是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另外, 由于使用者自己也可以设置计划任务,所 ...

  2. Oracle通过JOB定时执行存储过程实现两表数据比对

    需求: 第三方云平台管理的虚拟机会进行关机.资源扩展等操作,因此开关机状态.CPU.内存.磁盘大小等数据需要进行同步.这里第三方云平台是BMC CLM云平台,底层虚拟化平台是Vcenter.进行同步的 ...

  3. 【转】linux 定时执行shell脚本

    在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程,在像备份等需要在操作系统级定时任务只能采用crontab来完成 本文讲述crontab具体用法,以供备忘. 在oracle ...

  4. Linux下定时执行脚本(转自Decode360)

    文章来自:http://www.blogjava.net/decode360/archive/2009/09/18/287743.html Decode360's Blog  老师(业精于勤而荒于嬉 ...

  5. 如何使用Linux的Crontab定时执行PHP脚本的方法

    我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...

  6. Linux下的crontab定时执行任务命令详解

    在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“cr ...

  7. crontab定时执行任务

    第一部分 crontab介绍 每个操作系统都有它的自动定时启动程序的功能,Windows有它的任务计划,而Linux对应的功能是crontab. crontab简介 crontab命令常见于Unix和 ...

  8. 关于oracle dbms_job 定时执行的内容。

    一.设置初始化参数 job_queue_processessql> alter system set job_queue_processes=n;(n>0)job_queue_proces ...

  9. linux 定时执行shell脚本

    转自:http://blog.csdn.net/jingxiangren/article/details/4745631 在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程, ...

随机推荐

  1. Java垃圾回收(GC)与引用的种类

    垃圾回收 GC public class MyObject {     private String id;     public MyObject(String id) {         this ...

  2. Android SQLite 数据库详细介绍

    Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...

  3. sql 自定义函数--十进制转二进制

    随笔记忆: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DECTOBIN]') and xtype in ...

  4. mybatis缓存创建过程

    带着 上篇 的问题,再来看看mybatis的创建过程 1.从SqlSessionFactoryBuilder解析mybatis-config.xml开始 对文件流解析 XMLConfigBuilder ...

  5. RedHat7搭建yum源服务器

    1.新建目录 # mkdir -p /content/rhel7/x86_64/{isos,dvd}/ 2.上传RedHat安装光盘镜像,上传后的路径为 /content/rhel7/x86_64/i ...

  6. 查锁表及kill

    当一个表一直被锁住而无法进行操作的时候,可以用如下方法 select l.session_id sid, s.serial#, l.locked_mode 锁模式, l.oracle_username ...

  7. Angular 2.0 从0到1 (四)

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  8. web前端常用小函数汇总

    //去掉html标签 function delHtmlTag(str) { var title = str.replace(/<[^>]+>/g, "");// ...

  9. SQL 数据库基础语句

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建备份 ...

  10. crackme_zapline分析

    [破文标题]crackme_zapline 分析 [破文作者]CloAk [作者邮箱]@qq.com [作者主页] [破解工具]OD,... [破解平台]Windows --------------- ...