目的实现一个间隔轮询执行的功能。

从网上看到java中可以使用Timer,于是本篇文件就对自己的使用记录,进行一次记录。

主函数:

 package cn.test.timer;

 import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; public class TestTimer {
public static void main(String[] args) {
Timer timer = new Timer();
// schedule task
timer.schedule(new MyTimerTask(), 0, 2000); System.out.println("task over time:" + new Date()); try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("task over time:" + new Date());
} }

继承TimerTask的自定义类MyTimerTask:

 package cn.test.timer;

 import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask; import org.apache.log4j.Logger; public class MyTimerTask extends TimerTask {
private final Logger logger = Logger.getLogger(MyTimerTask.class.getName());
private final SimpleDateFormat fullDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
private final SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd"); @Override
public void run() {
// TODO Auto-generated method stub
System.out.println("task run time:" + new Date()); logger.info("start............................."); String fileDir = "D:/Java_Study/windowsservice/JavaService_2_0_10-amd64/log/";
String filePath = fileDir + shortDateFormat.format(new Date()) + ".txt"; File logDirector = new File(fileDir);
File logFile = new File(filePath); try {
if (!logDirector.exists()) {
logDirector.createNewFile();
}
if (!logFile.exists()) {
logFile.createNewFile();
}
FileWriter writer = new FileWriter(filePath, true); writer.write(fullDateFormat.format(new Date())+"\r\n");
writer.close(); } catch (final IOException e) {
e.printStackTrace();
logger.error("error:\r\n" + e.getMessage() + "\r\n" + e.getStackTrace());
} finally { }
logger.info("end.............................");
} }

运行起来后,Console上输出信息为:

task over time:Fri Apr 22 23:53:15 CST 2016
task run time:Fri Apr 22 23:53:15 CST 2016
0 INFO 2016-04-22 23:53:15 cn.test.timer.MyTimerTask start.............................
1 INFO 2016-04-22 23:53:15 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:17 CST 2016
1998 INFO 2016-04-22 23:53:17 cn.test.timer.MyTimerTask start.............................
2000 INFO 2016-04-22 23:53:17 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:19 CST 2016
3998 INFO 2016-04-22 23:53:19 cn.test.timer.MyTimerTask start.............................
4001 INFO 2016-04-22 23:53:19 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:21 CST 2016
5998 INFO 2016-04-22 23:53:21 cn.test.timer.MyTimerTask start.............................
6001 INFO 2016-04-22 23:53:21 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:23 CST 2016
7998 INFO 2016-04-22 23:53:23 cn.test.timer.MyTimerTask start.............................
task over time:Fri Apr 22 23:53:23 CST 2016
8000 INFO 2016-04-22 23:53:23 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:25 CST 2016
9998 INFO 2016-04-22 23:53:25 cn.test.timer.MyTimerTask start.............................
9999 INFO 2016-04-22 23:53:25 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:27 CST 2016
11999 INFO 2016-04-22 23:53:27 cn.test.timer.MyTimerTask start.............................
12000 INFO 2016-04-22 23:53:27 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:29 CST 2016
13999 INFO 2016-04-22 23:53:29 cn.test.timer.MyTimerTask start.............................
14001 INFO 2016-04-22 23:53:29 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:31 CST 2016
15999 INFO 2016-04-22 23:53:31 cn.test.timer.MyTimerTask start.............................
16000 INFO 2016-04-22 23:53:31 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:33 CST 2016
17999 INFO 2016-04-22 23:53:33 cn.test.timer.MyTimerTask start.............................
18001 INFO 2016-04-22 23:53:33 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:35 CST 2016
19999 INFO 2016-04-22 23:53:35 cn.test.timer.MyTimerTask start.............................
20001 INFO 2016-04-22 23:53:35 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:37 CST 2016
21999 INFO 2016-04-22 23:53:37 cn.test.timer.MyTimerTask start.............................
22001 INFO 2016-04-22 23:53:37 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:39 CST 2016
23999 INFO 2016-04-22 23:53:39 cn.test.timer.MyTimerTask start.............................
24003 INFO 2016-04-22 23:53:39 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:41 CST 2016
25999 INFO 2016-04-22 23:53:41 cn.test.timer.MyTimerTask start.............................
26001 INFO 2016-04-22 23:53:41 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:43 CST 2016
27999 INFO 2016-04-22 23:53:43 cn.test.timer.MyTimerTask start.............................
28002 INFO 2016-04-22 23:53:43 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:45 CST 2016
30000 INFO 2016-04-22 23:53:45 cn.test.timer.MyTimerTask start.............................
30002 INFO 2016-04-22 23:53:45 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:47 CST 2016
32000 INFO 2016-04-22 23:53:47 cn.test.timer.MyTimerTask start.............................
32004 INFO 2016-04-22 23:53:47 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:49 CST 2016
34000 INFO 2016-04-22 23:53:49 cn.test.timer.MyTimerTask start.............................
34002 INFO 2016-04-22 23:53:49 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:51 CST 2016
36000 INFO 2016-04-22 23:53:51 cn.test.timer.MyTimerTask start.............................
36002 INFO 2016-04-22 23:53:51 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:53 CST 2016
38000 INFO 2016-04-22 23:53:53 cn.test.timer.MyTimerTask start.............................
38001 INFO 2016-04-22 23:53:53 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:55 CST 2016
40000 INFO 2016-04-22 23:53:55 cn.test.timer.MyTimerTask start.............................
40002 INFO 2016-04-22 23:53:55 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:57 CST 2016
42000 INFO 2016-04-22 23:53:57 cn.test.timer.MyTimerTask start.............................
42002 INFO 2016-04-22 23:53:57 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:53:59 CST 2016
44000 INFO 2016-04-22 23:53:59 cn.test.timer.MyTimerTask start.............................
44004 INFO 2016-04-22 23:53:59 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:01 CST 2016
46000 INFO 2016-04-22 23:54:01 cn.test.timer.MyTimerTask start.............................
46001 INFO 2016-04-22 23:54:01 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:03 CST 2016
48001 INFO 2016-04-22 23:54:03 cn.test.timer.MyTimerTask start.............................
48003 INFO 2016-04-22 23:54:03 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:05 CST 2016
50001 INFO 2016-04-22 23:54:05 cn.test.timer.MyTimerTask start.............................
50002 INFO 2016-04-22 23:54:05 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:07 CST 2016
52001 INFO 2016-04-22 23:54:07 cn.test.timer.MyTimerTask start.............................
52003 INFO 2016-04-22 23:54:07 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:09 CST 2016
54001 INFO 2016-04-22 23:54:09 cn.test.timer.MyTimerTask start.............................
54003 INFO 2016-04-22 23:54:09 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:11 CST 2016
56001 INFO 2016-04-22 23:54:11 cn.test.timer.MyTimerTask start.............................
56002 INFO 2016-04-22 23:54:11 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:13 CST 2016
58001 INFO 2016-04-22 23:54:13 cn.test.timer.MyTimerTask start.............................
58003 INFO 2016-04-22 23:54:13 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:15 CST 2016
60001 INFO 2016-04-22 23:54:15 cn.test.timer.MyTimerTask start.............................
60003 INFO 2016-04-22 23:54:15 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:17 CST 2016
62001 INFO 2016-04-22 23:54:17 cn.test.timer.MyTimerTask start.............................
62003 INFO 2016-04-22 23:54:17 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:19 CST 2016
64002 INFO 2016-04-22 23:54:19 cn.test.timer.MyTimerTask start.............................
64004 INFO 2016-04-22 23:54:19 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:21 CST 2016
66002 INFO 2016-04-22 23:54:21 cn.test.timer.MyTimerTask start.............................
66004 INFO 2016-04-22 23:54:21 cn.test.timer.MyTimerTask end.............................
task run time:Fri Apr 22 23:54:23 CST 2016
68002 INFO 2016-04-22 23:54:23 cn.test.timer.MyTimerTask start.............................
68003 INFO 2016-04-22 23:54:23 cn.test.timer.MyTimerTask end.............................

Timer的用法的更多相关文章

  1. .NET中的Timer类型用法详解

    这篇文章主要介绍了.NET中的Timer类型用法,较为详细的分析了Timer类型在各种环境下的用法,需要的朋友可以参考下   在.NET FrameWork中有多个Timer,那么怎么根据实际情况进行 ...

  2. Java中Timer的用法

    现在项目中用到需要定时去检查文件是否更新的功能.timer正好用于此处. 用法很简单,new一个timer,然后写一个timertask的子类即可. 代码如下: package comz.autoup ...

  3. Java中Timer的用法--转载之网络

    用法很简单,new一个timer,然后写一个timertask的子类即可. import java.util.Timer; import java.util.TimerTask; public cla ...

  4. timer.Interval用法简介

    这个东东呢是我在做windows服务的时候碰到的,总结了一下她的用法,如下: 一.指定时间间隔 写一个每隔一分钟就执行一次的吧 public partial class PSJCService : S ...

  5. handler和Timer的用法

    final Handler handler = new Handler(){public void handleMessage(Message msg){if (msg.what == 0x123){ ...

  6. C# Timer定时器用法

    System.Timers.Timer timer1 = new System.Timers.Timer(); timer1.Elapsed += new System.Timers.ElapsedE ...

  7. android Timer TimerTask用法笔记

    Android中经常会遇到执行一些周期性定时执行的任务.初学的时候经常会使用Thread.sleep()方法.在android中,有Timer可以专门干这个事情. 先看看Timer.class中都是些 ...

  8. 【WIP】swift3的timer的用法

    创建: 2017/10/14   更新: 2017/10/14 标题加上[WIP],补充创建时间     回家再写

  9. Timer和TimerTask的用法

    最近在做java课程设计的时候,我用到了timer,于是学习了一下timer的用法. java实现多线程比较常用的两种方法,一种是直接继承Thread类,另一种则是实现Runnable接口.Timer ...

随机推荐

  1. 移动端开发——javascript

    javascript(简称js)语言在移动前端应用很广.可以说必不可少,许多效果都是和js相关的.包括现在移动端的一些框架.jqmobi.jqtouch.sencha touch.jquerymobi ...

  2. 【BZOJ】1532: [POI2005]Kos-Dicing

    题意 \(n\)个人\(m\)场比赛\((1 \le n \le 10000, 0 \le m \le 10000)\),给出每场比赛的两个选手,求赢得最多的人最少赢的场数. 分析 二分最多人赢的场数 ...

  3. 【POJ】1739 Tony's Tour

    http://poj.org/problem?id=1739 题意:n×m的棋盘,'#'是障碍,'.'是空白,求左下角走到右下角且走过所有空白格子的方案数.(n,m<=8) #include & ...

  4. jQuery的Dom插入操作图示

  5. Bootstrap_让Bootstrap轮播插件carousel支持左右滑动手势的三种方法

    Bootstrap 的 carousel.js 插件并没有支持手势. 3种解决方案 : jQuery Mobile (http://jquerymobile.com/download/) $(&quo ...

  6. php by oneself

    在php里面写html代码真的很麻烦,最近学到了一个新的方法: <html> <head> <title>PHP</title> <meta ht ...

  7. linux shell 获取进程pid

    1.通过可执行程序的程序名称 a.运行程序 b.获取进程id号 c.pidof相关知识:http://www.cnblogs.com/yunsicai/p/3675938.html 2.有些程序需要在 ...

  8. 快排java实现

    package sort; public class QuickSort { public static final int cutoff = 3; /** * insertion sort * * ...

  9. oracle 字符乱码问题解决方案

    今天在客户服务器上遇到了oracle中文乱码问题,第一个想到的是:要想避免oracle字符乱码的问题,需要注意oracle客户端的字符编码和服务端的字符编码保持一致. 于是操作如下: 1.查看服务端字 ...

  10. php 中如何创建一个空对象

    // 创建一个空对象 $obj=(object)array(); //假设这是从数据库取内容 $arr=["prod_id"=>103,"prod_name&quo ...