TimerTask与Timer设计时间监控
来源:互联网 发布:成都网络维护培训学校 编辑:程序博客网 时间:2024/05/19 06:48
今天接到一个新需求,在某个时间点,提前15分钟输出日志,然后提前10分钟再输出日志然后提前5分钟再输出日志。
想了想,决定用Java的TimerTask与Timer进行设计。本人也是第一次使用,所以写了个测试类,给大家分享:
第一个类:
package timertask.usable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* <Description functions in a word>
* <Detail description>
*
* @author Cookie Xu
* @version [Version NO, 2013-6-28]
* @see [Related classes/methods]
* @since [product/Modul version]
*/
public class Test {
Timer timer;
public Test(Date date,String output) {
timer = new Timer();
RemindTask rt = new RemindTask();
rt.setOutput(output);
timer.schedule(rt, date);
}
class RemindTask extends TimerTask {
private String output = "";
public void setOutput(String output) {
this.output = output;
}
public void run() {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(output);
System.out.println("Output Logs………………………… Time : "+dfs.format(new Date()));
timer.cancel();
}
}
private static List<Date> getDateList() throws Exception {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = dfs.parse("2013-06-28 16:57:18");
Date date2 = dfs.parse("2013-06-28 16:56:59");
Date date3 = dfs.parse("2013-06-28 16:56:43");
Date date4 = dfs.parse("2013-06-28 16:56:30");
List<Date> date = new ArrayList<Date>();
date.add(date1);
date.add(date2);
date.add(date3);
date.add(date4);
return date;
}
public static void main(String[] args) throws Exception {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Date> d = getDateList();
for(int i = 0;i < d.size();i++){
Date date = d.get(i);
List<Date> dateList = GetPointInTime.getPointInTime(date);
for (int j = 0; j < dateList.size(); j++) {
Date endDate = dateList.get(j);
int z = i + 1;
System.out.println("Need Output Logs Time:"+ z +" ------" + dfs.format(endDate));
new Test(endDate,"第" + z + "个定时器:");
}
}
}
}
第二个类
package timertask.usable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* <Description functions in a word>
* <Detail description>
*
* @author Cookie Xu
* @version [Version NO, 2013-6-28]
* @see [Related classes/methods]
* @since [product/Modul version]
*/
public class GetPointInTime {
/** <Description functions in a word>
* <Detail description>根据参数,得到一段事件以前
* @param date 时间点
* @param judgeChange判断提前的位置
* @param changeNumber提前的计算
* @return
* @throws Exception [Parameters description]
*
* @return Date [Return type description]
* @exception throws [Excetion] [Exception description]
* @see [Related classes#Related methods#Related properties]
*/
private static Date getFewAgo(Date date,String judgeChange,int changeNumber) throws Exception{
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//得到年月日
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//得到分钟
int minutes = calendar.get(Calendar.MINUTE);
//得到秒
int seconds = calendar.get(Calendar.SECOND);
if(judgeChange.equals("minutes")){
minutes = minutes + changeNumber;
}else if(judgeChange.equals("seconds")){
seconds = seconds + changeNumber;
}
calendar.set(year, month, day, hour, minutes, seconds);
return calendar.getTime();
}
/** <Description functions in a word>
* <Detail description>得到时间点数组
* @param date
* @return
* @throws Exception [Parameters description]
*
* @return List<Date> [Return type description]
* @exception throws [Excetion] [Exception description]
* @see [Related classes#Related methods#Related properties]
*/
public static List<Date> getPointInTime(Date date) throws Exception{
List<Date> dateList = new ArrayList<Date>();
Date changeDate1 = getFewAgo(date,"minutes",-1);
Date changeDate2 = getFewAgo(date,"seconds",-30);
Date changeDate3 = getFewAgo(date,"seconds",-10);
Date changeDate4 = getFewAgo(date,"seconds",-5);
Date changeDate5 = getFewAgo(date,"seconds",-2);
dateList.add(changeDate2);
dateList.add(changeDate3);
dateList.add(changeDate4);
dateList.add(changeDate5);
return dateList;
}
public static void main(String[] args) throws ParseException {
}
}
- TimerTask与Timer设计时间监控
- Timer与TimerTask入门
- Timer与TimerTask入门
- Timer与TimerTask
- Timer 与 TimerTask 类
- Timer与TimerTask详解
- Timer与TimerTask入门
- timerTask与Timer
- Timer与TimerTask
- Timer 与 TimerTask
- Timer与TimerTask
- Timer与TimerTask相关
- Timer与TimerTask
- Timer/TimerTask与ScheduledExecutorService
- Timer与TimerTask
- 简单了解Timer与TimerTask
- 简单了解Timer与TimerTask
- 简单了解Timer与TimerTask
- Ubuntu和Windows7双系统,修改启动顺序问题
- Centos yum 安装nginx+php+mysql
- 第一章 引言--《设计模式-可复用面向对象软件的基础》Erich Gamma
- git无法提交出错的问题22,411,413
- oracle 11g 远程导出数据库
- TimerTask与Timer设计时间监控
- C++异常机制的实现方式和开销分析
- SQL 2008 Installation error: Impossible de charger le fichier ou l'assembly System, Version=4.0.0.0
- Sql语句之left/right join inner join
- 做好seo真很需要耐性
- 压缩文件夹代码
- Fiddler 教程
- 【科研论文】基于HTTP协议的嵌入式远程监控系统的研究
- 屏知识普及以及屏的硬件相关