• 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 }
  • 相关阅读:
    Win10系统接口自动化测试持续集成
    postman结合newman生成测试报告
    关于sass(scss)、less、postcss、stylus的简介与区别
    python 对pdf的response进行操作(如:去掉首页)
    存数据的格式
    json文本格式化
    python处理时间
    @SuppressWarning
    【源码笔记】构建Spring源码环境
    手写一个模拟的ReentrantLock
  • 原文地址:https://www.cnblogs.com/xuzhaocai/p/8146576.html
Copyright © 2020-2023  润新知