java定时器用法

1、代码示例

//继承TimerTask
public class TestTimerTask extends TimerTask{

    public void run() {

        while (true) {
            boolean result = getFlag();
            if (result) {
                break;
            }
            try {
                Thread.sleep(1000 * 60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("rfm数据返回ok,现在时间是:" + Calendar.getInstance().getTime());
        //需要执行的任务
    }

    public boolean getFlag(){
        Connection connn = null;
        PreparedStatement pss = null;
        ResultSet rss = null;
        boolean flag = false;
        ObjectMapper objectMapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .setDateFormat(new SimpleDateFormat("yyyy-MM-dd"))
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)                         .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date1 = null;

            Date date = Calendar.getInstance().getTime();
            date1 = simpleDateFormat.format(date);

            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://xxxxx";
            connn = DriverManager.getConnection(url, "xxxx", "xxxx");

            pss = connn.prepareStatement("select * from xxxx");
            rss = pss.executeQuery();
            Map flagMap = new HashMap<>();
            while (rss.next()) {
                flagMap.put(rss.getString(2), rss.getString(1));
            }

            System.out.println(objectMapper.writeValueAsString(flagMap));
            String flags = "";
            try {
                flags = flagMap.get(date1);
            } catch (NullPointerException nu) {

            }
            if("1".equals(flags)){
                flag = true;
            }

        }catch (Exception e) {

        } finally{
            try {
                if(rss != null) {
                    rss.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(pss != null) {
                    pss.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(connn != null) {
                    connn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  flag;
    }

    public static void main (String[] ages){

        Timer timer = new Timer();//定时器
        TestTimerTask ts = new TestTimerTask();//定时任务目标
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 6); //早晨6点
        calendar.set(Calendar.MINUTE, 20);
        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        long period = 1000 * 60 * 60 * 24;//执行间隔24小时

        //如果第一次执行定时任务的时间 小于当前的时间
        //此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
        if (date.before(new Date())) {
            date = addDay(date, 1);
        }

        timer.scheduleAtFixedRate(ts, date, period);//设置执行参数并且开始定时任务
    }

    // 增加或减少天数
    public static Date addDay(Date date, int num) {
        Calendar startDT = Calendar.getInstance();
        startDT.setTime(date);
        startDT.add(Calendar.DAY_OF_MONTH, num);
        return startDT.getTime();
    }
}

你可能感兴趣的:(Java)