java定时器,服务器启动时执行

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hoochiang/article/details/84417202
登录 注册 帮助



首页
分享
寻求
分类
标签


寻求代码


分享代码



"/1000879008580608.java"的全部代码

package com.ictehi.util;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ictehi.bean.XSSLData;
import com.ictehi.jdbc.JdbcHelp;

public class MyServlet extends HttpServlet{

private static final long serialVersionUID = 1L;
Timer timer;

public MyServlet(){
timer = new Timer();
}

public void init() throws ServletException{
timer.schedule(new MyTask(), 1000, 600000); //10min执行一次MyTask(),
timer.schedule(new MyTask1(), System.currentTimeMillis()-System.currentTimeMillis()%3600000+3600000, 3600*1000);

}

public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException{
}

public void destory(){
timer.cancel();
}

/**
* 整点取一次雨量、水位数据,存放到新表。2012年10月11日8:55:50 ypc
* @author Administrator
*
*/


public static void main(String[] args) {
System.out.println(System.currentTimeMillis()-System.currentTimeMillis()%6000+6000);
}

}


timer定时执行任务,tomcat启动时执行
展开阅读全文

java定时器???

10-06

rnTimerTask中scheduledExecutionTime()API描述是这样的:rn[color=#FF0000][b][size=16px]Returns the scheduled execution time of the most recent actual execution of this task. (If this method is invoked while task execution is in progress, the return value is the scheduled execution time of the ongoing task execution.)rnrnThis method is typically invoked from within a task's run method, to determine whether the current execution of the task is sufficiently timely to warrant performing the scheduled activity:rnrn public void run() rn if (System.currentTimeMillis() - scheduledExecutionTime() >=rn MAX_TARDINESS)rn return; // Too late; skip this execution.rn // Perform the taskrn rn rnrnThis method is typically not used in conjunction with fixed-delay execution repeating tasks, as their scheduled execution times are allowed to drift over time, and so are not terribly significant.[/size][/b][/color]rn我比较困惑的是System.currentTimeMillis() - scheduledExecutionTime()这一用法。rnscheduled execution time是不是与actual execution time不同??rn假设在时刻t,TimerTask第一次被调用执行,然后按照Timer类的scheduleAtFixedRate(task,t,delt)进行fixed-rate的重复执行。那么scheduled execution time应该是t+delt,t+2*delt,...,t+n*delt,而且是确定的,只要到这个时间点就会发生scheduled execution(这里不考虑时钟的绝对精确性问题),至于task实际的执行时间则是未知的:比如在t+delt时刻发生了scheduled execution,但由于某种原因,task调用被推迟了,到t+1.5*delt时刻才真正执行。rn那么在这种情况下scheduledExecutionTime()的返回值对应的t+delt而非t+1.5*delt。假设MAX_TARDINESS=0.2delt,这时System.currentTimeMillis() - scheduledExecutionTime()=0.5delt,超过了最大延迟0.2delt,然后task任务就应该被迫放弃本次执行。rnrn不知道,我的理解对与不对,求大牛指点。。。rnrnPS: API SPECS中有一句话是:The return value is undefined if the task has yet to commence its first execution.不明白这句话想表达什么意思。我的理解是将if换成when后的意思。rnrn谢谢!! 论坛

没有更多推荐了,返回首页