zoukankan      html  css  js  c++  java
  • java学习笔记之定时器

    定时器

     1 package pack01_timer;
     2 
     3 import java.io.File;
     4 import java.text.ParseException;
     5 import java.text.SimpleDateFormat;
     6 import java.util.Date;
     7 import java.util.Timer;
     8 import java.util.TimerTask;
     9 
    10 class MyTimer extends TimerTask{
    11     private Timer t;
    12     
    13     public MyTimer(Timer t) {
    14         super();
    15         this.t = t;
    16     }
    17 
    18     public MyTimer() {
    19         super();
    20         // TODO Auto-generated constructor stub
    21     }
    22 
    23     @Override
    24     public void run() {
    25 //        File file = new File("D:\demo");
    26 //        File[] listFiles = file.listFiles();
    27 //        for (File file2 : listFiles) {
    28 //            if(file2.getName().endsWith(".xml")){
    29 //                System.out.println("自动解析xml");
    30 //                t.cancel();
    31 //            }
    32 //        }
    33         System.out.println("时间到了,开始解析");
    34     }
    35     
    36 }
    37 public class DemoTimer {
    38     public static void main(String[] args) throws ParseException {
    39         //Timer类
    40         
    41         Timer t = new Timer();
    42         /*
    43          * 参1:抽象类,这个抽象类用来指定时间到了该干什么事情
    44          * 参2:定时的时间:单位是毫秒
    45          */
    46 //        t.schedule(new MyTimer(), 3000);
    47         //参3:每隔一定时间又去做这件事
    48 //        t.schedule(new MyTimer(t), 3000, 200); 
    49         
    50         String str = "2017-12-29 18:47:15";
    51         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    52         Date date = sdf.parse(str);
    53         t.schedule(new MyTimer(), date);
    54         
    55         //取消定时器
    56 //        t.cancel();
    57     }
    58 }
  • 相关阅读:
    VC++ 在Watch窗口显示GetLastError值以及详细信息
    VC++ Debug内存值
    VC++ 给你的代码强制加一个硬断点
    wchat_t与char互转
    使用forever运行nodejs应用
    C++ 检查Windows服务运行状态
    【转】Android横竖屏重力自适应
    可配置多功能门 SN74LVC1G57, 1G58, 1G97, 1G98, 1G99
    VPW协议解析
    STM32的TAMPER-RTC管脚作为Tamper使用
  • 原文地址:https://www.cnblogs.com/xuzhaocai/p/8146576.html
Copyright © 2011-2022 走看看