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<String, String> 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();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值