1.使用java并发库编写定时程序。 如定时上传文件,定时删除日志,定时执行sql 等
下面的程序在每天的23:20:30 后,执行一次具体任务
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MyTimer implements Runnable{
//执行标志位 true:可以执行,false:不可以执行
private boolean flag = true;
public int getCurrTime(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
return Integer.valueOf(sdf.format(cal.getTime()));
}
@Override
public void run() {
//转天状态改变
if((getCurrTime() < 1)&&(!flag)){
System.out.println("======"+getCurrTime());
flag = true;
}
//ball 模式
if(getCurrTime() < 232030){
System.out.println("return!!!");
return ;
}
if(flag){
//执行具体任务
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("cur date::"+sdf.format(cal.getTime()));
flag = false;
}else{
System.out.println("==============false-------------");
}
}
public static void main(String args[]) {
try {
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleAtFixedRate(new MyTimer(), 1, 2, TimeUnit.SECONDS);
} catch (RejectedExecutionException e) {
e.printStackTrace();
}
}
}
相关推荐
主要介绍了使用java执行定时任务示例,需要的朋友可以参考下
主要介绍了java Timer 定时每天凌晨1点执行任务的代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
通过配置数据库连接信息和要执行的sql语句,可实现定时执行多个sql语句。 所要执行的语句只能是写死的,可支持sqlserver mysql oracle。 配置说明: config/sys.properties 中指定数据库类型及连接信息,执行间隔...
NULL 博文链接:https://penghuaiyi.iteye.com/blog/353669
定时任务在项目中经常会使用到,本文主要根据博主自己使用定时的经验分如下几点介绍定时任务:1、Quartz定时任务简介及Spring配置Quartz定时任务;2、SchedulerFactory对定时任务进行增删改查
java定时任务,每天定时执行任务,每天到这个时间点都会执行
spring+quartz整合,动态管理定时任务,可对正在进行的任务操作增删改查,可配合数据库进行操作
介绍了Java定时执行任务,给出了如何使用ServletContextListener实现定时运行java某个方法的例子,简单易用。
java定时任务,每天定时执行任务.java定时任务,每天定时执行任务.