Timer的用法

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

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

主函数:

 1 package cn.test.timer;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.OutputStreamWriter;
 6 import java.text.SimpleDateFormat;
 7 import java.util.Date;
 8 import java.util.Timer;
 9 import java.util.TimerTask;
10 
11 public class TestTimer {
12     public static void main(String[] args) {
13         Timer timer = new Timer();
14         // schedule task
15         timer.schedule(new MyTimerTask(), 0, 2000);
16 
17         System.out.println("task over time:" + new Date());
18 
19         try {
20             Thread.sleep(8000);
21         } catch (InterruptedException e) {
22             e.printStackTrace();
23         }
24 
25         System.out.println("task over time:" + new Date());
26     }
27 
28 }

继承TimerTask的自定义类MyTimerTask:

 1 package cn.test.timer;
 2 
 3 import java.io.*;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 import java.util.TimerTask;
 7 
 8 import org.apache.log4j.Logger;
 9 
10 public class MyTimerTask extends TimerTask {
11     private final Logger logger = Logger.getLogger(MyTimerTask.class.getName());
12     private final SimpleDateFormat fullDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
13     private final SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd");
14 
15     @Override
16     public void run() {
17         // TODO Auto-generated method stub
18         System.out.println("task run time:" + new Date());
19 
20         logger.info("start.............................");
21 
22         String fileDir = "D:/Java_Study/windowsservice/JavaService_2_0_10-amd64/log/";
23         String filePath = fileDir + shortDateFormat.format(new Date()) + ".txt";
24 
25         File logDirector = new File(fileDir);
26         File logFile = new File(filePath);
27 
28         try {
29             if (!logDirector.exists()) {
30                 logDirector.createNewFile();
31             }
32             if (!logFile.exists()) {
33                 logFile.createNewFile();
34             }
35             FileWriter writer = new FileWriter(filePath, true);
36 
37             writer.write(fullDateFormat.format(new Date())+"\r\n");
38             writer.close();
39 
40         } catch (final IOException e) {
41             e.printStackTrace();
42             logger.error("error:\r\n" + e.getMessage() + "\r\n" + e.getStackTrace());
43         } finally {
44 
45         }
46         logger.info("end.............................");
47     }
48 
49 }

运行起来后,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.............................

posted @ 2016-04-23 00:03  cctext  阅读(404)  评论(0编辑  收藏  举报