java 精确定时_定时任务,客户页面操作,使用java.util.timer(要求时间精确度不高的定时任务)...

//线程安全map:ConcurrentHashMap

private Map timerTaskMap ;//

private Timer timer ;//不能放到方法里边,new 一个启动一个线程//启动了一个新线程,这个新线程并不是守护线程,所以会一直运行

publicTimer getTimer() {returntimer;

}public voidsetTimer(Timer timer) {this.timer =timer;

}//AutoEvalTask task = new AutoEvalTask();//不能放到方法里边,

/*** 启动所有定时任务

*@paramfirstTimeStr

*@paramperiodStr 秒数

*@return

*/@RequestMapping(value="/lanchAllTimerTask")

@ResponseBodypublic MaplanchAllTimerTask() {

Map res = new HashMap();try{

Map hmget = redisUtil.hmget("seller_schedule_time_list");for (Entrymap : hmget.entrySet()) {

String key= (String) map.getKey();//username+taskName

UserScheduleTimes value = (UserScheduleTimes) map.getValue();//定时任务信息对象

if(CollectionUtils.isEmpty(timerTaskMap)) {

timerTaskMap= new ConcurrentHashMap();

}

AutoEvalTask task=timerTaskMap.get(key);if (task!=null) {

task.cancel();//从定时任务队列中移除

}if (timer==null) {

timer= new Timer();//只有一个此定时任务队列。

}

task= new AutoEvalTask();//定时任务对象//task = SpringContextHolder.getBean("autoEvalTask");//会报异常 java.lang.IllegalStateException: Task already scheduled or cancelled

task.setUserScheduleTimes(value);

timerTaskMap.put(key, task);if (value !=null) {

Date HHmmDate=value.getFirstTime();

Date executeDate=getExecuteDate(HHmmDate);

timer.schedule(task, executeDate, value.getPeriod()*1000);//如果时间在当前时间之前,立即执行。然后间隔时间再次执行。

}

LOGGER.info("key:"+key+",value:"+value);

}

res.put("status", "succ");

}catch(Exception e) {

LOGGER.info("lanchAllTimerTask方法异常!",e);

e.printStackTrace();

res.put("status", "fail");

}returnres;

}/*** 获取执行定时任务时刻,如果是当前时刻之前 第二天此时刻开始执行

*@paramHHmmDate

*@return

*/

privateDate getExecuteDate(Date HHmmDate) {

Calendar now=Calendar.getInstance();

Date executeDate= new Date(now.get(Calendar.YEAR) - 1900, now.get(Calendar.MONTH),

now.get(Calendar.DAY_OF_MONTH), HHmmDate.getHours(), HHmmDate.getMinutes(), HHmmDate.getSeconds());if (executeDate.compareTo(new Date())<0) {//不加此设置,如果是当前时刻 立即执行

executeDate = DatesUtils.getTimePeriodBeforeDate(-1, executeDate);//1天后的此时刻执行任务。(最准确是开始时间+间隔倍数 大于当前时间最近的时间)

}returnexecuteDate;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JAVA精确定时器,利用系统时间,使长期工作的误差稳定。 功能: ·可定时启动任务或直接启动任务 ·重复启动任务时间间隔可在任务线程中改变,范围大于100ms,否则精度降低) 引用列表: ·import psn.razerpen.thread.AccuracyTimer; ·import psn.razerpen.thread.AccuracyTimerMission; ·import psn.razerpen.time.TimeStruct; 使用方法: //1·继承AccuracyTimerMission接口,创建一个类。 class MyTimer implements AccuracyTimerMission { //2·指定一个周期 int nDelay=1000; //3·重写run方法(如不需要使用新线程执行任务,也可留空) /** * 任务线程,本函数继承自Runnable */ @Override public void run() { System.out.println(new TimeStruct()); } //4·重写RunInCurrentThread(long nCurrentMilliSecond)方法。该方法接收当前时间,并返回下一次执行时间。如果返回值不大于nCurrentMilliSecond则中止计时器。该方法必须重写。 /** * 接收当前时间的毫秒值,并返回下一次执行的毫秒值。如果返回的下一个时间早于当前时间,则退出 */ @Override public long RunInCurrentThread(long nCurrentMilliSecond) { return nCurrentMilliSecond+=nDelay; } } //5·创建主线程代码 public class TestTimer { public static void main(String[] args) throws InterruptedException { //6·创建一个AccuracyTimer对象,并指定一个任务。 AccuracyTimer at=new AccuracyTimer(new MyTimer()); //7·(可选)如果不需要在新线程中启动任务,则写 // at.SetNewThreadEnabled(false); //否则不写或者写 // at.SetNewThreadEnabled(true); //8·(可选)设定第一次启动的时间点SetNextMissionTime/SetNextMissionMilliSecond或延迟时间SetNextMissionMilliSecondFromNow //设置为当前这一分钟的第59秒后启动(不写此行则表示直接启动) at.SetNextMissionTime(Integer.MIN_VALUE, -1, -1, -1, -1, 59, 0); //9·启动定时器 at.Start(); //10·主线程继续 for(int i=0;i<60;++i){ Thread.sleep(1000); } //11·结束定时器 at.End(); } } 详见sample.razerpen.thread包中TestTimer及各代码文件中注释

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值