Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javaTimer perform tasks regularly?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to perform tasks regularly in javaTimer". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to perform tasks regularly in javaTimer".

Here's how java Timer performs tasks at 1: 00 a. M. every day. The specific code is as follows:

Import java.util.TimerTask;/** * execution content * @ author admin_Hzw * * / public class Task extends TimerTask {public void run () {System.out.println ("I have a little donkey!");}} import java.util.Calendar;import java.util.Date;import java.util.Timer / * Task Management * @ author admin_Hzw * * / public class TimerManager {/ * * @ param args * / public static void main (String [] args) {new TimerManager ();} / / interval (one day) private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; public TimerManager () {Calendar calendar = Calendar.getInstance (); calendar.set (Calendar.HOUR_OF_DAY, 1); / / 1 am calendar.set (Calendar.MINUTE, 0) Calendar.set (Calendar.SECOND, 0); Date date=calendar.getTime (); / / time for the first execution of the scheduled task / / if the time for the first execution of the scheduled task is less than the current time / / at this time, one day is added to the time for the first execution of the scheduled task, so that the task is executed at the next point in time. If you don't add one day, the task will be carried out immediately. If (date.before (new Date () {date = this.addDay (date, 1);} Timer timer = new Timer (); Task task = new Task (); / / schedule the specified task to be executed repeatedly with a fixed delay at the specified time. Timer.schedule (task,date,PERIOD_DAY);} / increase or decrease the number of days public Date addDay (Date date, int num) {Calendar startDT = Calendar.getInstance (); startDT.setTime (date); startDT.add (Calendar.DAY_OF_MONTH, num); return startDT.getTime ();}}

Knowledge point expansion: java implements scheduled tasks and executes tasks at a specified time every day.

The first step

Package com.eh.util;import java.util.Calendar;import java.util.Date;import java.util.Timer;/** * java scheduled tasks, daily scheduled tasks * @ author wls * * / public class TimerManager {/ / interval private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; public TimerManager () {Calendar calendar = Calendar.getInstance () / * customize daily 2:00 execution method * * / calendar.set (Calendar.HOUR_OF_DAY, 16); calendar.set (Calendar.MINUTE, 10); calendar.set (Calendar.SECOND, 0); Date date=calendar.getTime (); / / time of the first scheduled task execution System.out.println (date); System.out.println ("before method comparison:" + date.before (new Date () / / if the time to execute the scheduled task for the first time is less than the current time / / add one day to the time when the scheduled task is executed for the first time, so that the task can be executed at the next point in time. If you don't add one day, the task will be carried out immediately. The cycle of loop execution is based on the current time if (date.before (new Date () {date = this.addDay (date, 1); System.out.println (date);} Timer timer = new Timer (); NFDFlightDataTimerTask task = new NFDFlightDataTimerTask (); / / schedule the specified task to be executed with repeated fixed delays at the specified time. Timer.schedule (task,date,PERIOD_DAY);} / increase or decrease the number of days public Date addDay (Date date, int num) {Calendar startDT = Calendar.getInstance (); startDT.setTime (date); startDT.add (Calendar.DAY_OF_MONTH, num); return startDT.getTime ();}}

Part two

Package com.eh.util;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.TimerTask;/** * in the TimerManager category, you must pay attention to the timing. If you're set to perform the mission at 2: 00 in the morning. But if you released the program or restarted the service after 2: 00 a. M., the task will be executed immediately instead of waiting until 2: 00 am the next day. In order to avoid this situation, it can only be judged that if the release or restart of the service is later than the scheduled execution time of the task, add one day on this basis. * @ author wls * * / public class NFDFlightDataTimerTask extends TimerTask {private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); @ Override public void run () {try {/ / write what you want to execute here System.out.println ("execution current time" + formatter.format (Calendar.getInstance (). GetTime () } catch (Exception e) {System.out.println ("- an exception occurred in parsing information -");}

Part III

Package com.eh.util;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class NFDFlightDataTaskListener implements ServletContextListener {public void contextInitialized (ServletContextEvent sce) {new TimerManager ();} public void contextDestroyed (ServletContextEvent sce) {/ / TODO Auto-generated method stub}}

Step 4: configure the web.xml file

Com.eh.util.NFDFlightDataTaskListener thank you for your reading, the above is the content of "how to regularly execute tasks in javaTimer". After the study of this article, I believe you have a deeper understanding of how to regularly execute tasks in javaTimer, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report