Timer小工具

根据构造函数参数

1.开始时间

2.结束时间

3.轮训次数

4.轮训时间

纯在bug ,实体垃圾没有回收,轮训时间不准确

Task

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

import javax.management.timer.Timer;

/**  
 * 可动态修改的任务timer 
 */  
public class Task extends TimerTask {  
    int i = 0;
    @Override  
    public void run() {  
        int cycNum  = TimerEnv.cycNum--;
        String ed = TimerEnv.EndTime;
        Long dateLong = Long.valueOf(System.currentTimeMillis()); 
        if(cycNum == i ||ed.equals(new SimpleDateFormat("HH:mm:ss").format(new Date(dateLong)))){
            ((Task)TimerEnv.map.get("task")).cancel();
        }
        System.out.println("i = "+ cycNum-- +"  systime:"+System.currentTimeMillis());
        System.out.println("longToDate:"+new SimpleDateFormat("HH:mm:ss").format(new Date(dateLong))); 
        System.out.println(ed.equals(new SimpleDateFormat("HH:mm:ss").format(new Date(dateLong))));
       
    }  
    

}  

TimerEnv

import java.util.HashMap;
import java.util.Map;

public class TimerEnv {

    public static Map map = new HashMap();
    public static Task task = null;
    public static String StartTime;
    public static String EndTime;
    public static int cycNum = 0;
    public static long period;

}

TimerShedule

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

/**
 * 任务调度管理器
 */
public class TimerShedule {
    
    private  Timer timer = new Timer();
    private  Task task = null;
    private  String StartTime;
    private  String EndTime;
    private  int cycNum = 0;
    private  long period;
        
    public TimerShedule(){}
    
    public TimerShedule(String StartTime,String EndTime, int cycNum,long period){
        this.StartTime = StartTime;
        this.EndTime = EndTime;
        this.cycNum = cycNum;
        this.period = period;
        
        TimerEnv.StartTime = this.StartTime;
        TimerEnv.EndTime = this.EndTime;
        TimerEnv.cycNum = this.cycNum;
        TimerEnv.period = this.period;
        
    }


    @SuppressWarnings("unchecked")
    public void start() {
        Date st = convertDate(StartTime.split(":")[0],StartTime.split(":")[1],StartTime.split(":")[2]);
        task = new Task();
        timer.schedule(task, st, period); 
        TimerEnv.map.put("task", task); 
        TimerEnv.map.put("timer", timer);
    }
    
  
    

    /**
     * 清空timer
     */
    public void clean(Task task) {
        if (task != null) {
            task.cancel();
        }
        timer.purge();
    }

    /**
     * 停止任务
     */
    public void stop(Task task) {
        clean(task);
    }
    
    /**
     * 日期转换
     * 
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public Date convertDate(String hour, String minute, String sec) {
        int h = Integer.parseInt(hour);
        int m = Integer.parseInt(minute);
        int s = Integer.parseInt(sec);
        Date date = bookTime(h, m, s);
        return date;
    }
    /**
     * 设定时间
     * 
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public Date bookTime(int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        Date date = calendar.getTime();
        return date;
    }

    
}


Test

package com.neusoft.timer;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        new TimerShedule("08:48:30", "08:48:38", 10, 1000).start();
    }

}

结果

Timer小工具_第1张图片

你可能感兴趣的:([老达笔记]Java,Java杂谈笔记)