Get the App
SLTechnology News&Howtos  ›  Development  › 

How does javaTimer perform tasks regularly?

Shulou Source: shulou.com Published: 2022-06-03 14:06:14 11月12日 Update

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!

Tags: Tasks time first time plus one content situation learning days methods knowledge knowledge points problems delays services one end code information cycles basics that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL macOS Shulou Tech Info Shulou Information