java两种定时器
第一种:循环执行的程序
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* java定时器
* @author lin
*
*/
public class ScheduledExecutorTest {
private ScheduledExecutorService scheduExec;
public long start;
ScheduledExecutorTest(){
this.scheduExec = Executors.newScheduledThreadPool(2);
this.start = System.currentTimeMillis();
}
public void timerOne(){
scheduExec.schedule(new Runnable() {
public void run() {
throw new RuntimeException();
}
},1000,TimeUnit.MILLISECONDS);
}
public void timerTwo(){
scheduExec.scheduleAtFixedRate(new Runnable() {
public void run() {
//循环执行的代码
System.out.println("timerTwo invoked ....."+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
},0,1000,TimeUnit.MILLISECONDS);//0代表多久以后执行 1000代表多久执行一次 单位毫秒
}
public static void main(String[] args) {
ScheduledExecutorTest test = new ScheduledExecutorTest();
test.timerOne();
test.timerTwo();
}
}
第二种:定时任务
1.在项目的web.xml写监听器
<!-- 定时器 -->
<listener>
<listener-class>com.blue.common.task.NFDFlightDataTaskListener</listener-class>
</listener>
2.监听器 NFDFlightDataTaskListener
package com.blue.common.task;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class NFDFlightDataTaskListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
new TimerManager();
}
public void contextDestroyed(ServletContextEvent event) {
}
}
3.定时器 TimerManager
package com.blue.common.task;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class TimerManager {
//时间间隔
private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;
public TimerManager() {
Calendar calendar = Calendar.getInstance();
/*** 定制每日2:00执行方法 ***/
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date=calendar.getTime(); //第一次执行定时任务的时间
//如果第一次执行定时任务的时间 小于 当前的时间
//此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
if (date.before(new Date())) {
date = this.addDay(date, 1);
}
Timer timer = new Timer();
NFDFlightDataTimerTask task = new NFDFlightDataTimerTask();
//安排指定的任务在指定的时间开始进行重复的固定延迟执行。
timer.schedule(task,date,PERIOD_DAY);
}
// 增加或减少天数
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}
}
4.定时任务NFDFlightDataTimerTask
package com.blue.common.task;
import java.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.blue.common.util.SpringUtils;
import com.blue.system.service.ContentService;
public class NFDFlightDataTimerTask extends TimerTask {
private ContentService contentService=SpringUtils.getBean("contentService");
private Logger logger = LoggerFactory.getLogger(NFDFlightDataTimerTask.class);
/**
* 定时更新新闻的量
*/
@Override
public void run() {
try {
contentService.updateContentAmounts();//文章量
} catch (Exception e) {
logger.info("-------------定时任务发生异常--------------");
}
}
}