jdk scheduleAtFixedRate定时任务使用记录

tech2025-08-27  11

需求:定时启动任务检索库

代码如下:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(5); long oneDay = 24 * 60 * 60 * 1000; long initDelay = getTimeMillis("00:00:30") - System.currentTimeMillis(); initDelay = initDelay > 0 ? initDelay : oneDay + initDelay; executor.scheduleAtFixedRate(new Runnable() { public void run() { try { afterPropertiesSet(); } catch (Exception e) { e.printStackTrace(); } } }, initDelay, oneDay, TimeUnit.MILLISECONDS) private static long getTimeMillis(String time) { try { DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd"); java.util.Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time); return curDate.getTime(); } catch (ParseException e) { e.printStackTrace(); } return 0; }

借鉴

借鉴学习代码

Executors学习

效果图

最新回复(0)