1. Write a task
Package COM. boonya. timer; import Java. text. simpledateformat; import Java. util. date; import Java. util. timertask;/*** mytask. java * function: compile a scheduled task * @ author boonya * @ version 1.0 2012-10-11 */public class mytask extends timertask {@ override public void run () {// todo auto-generated method stub date mydate = new date (); string runtime = new simpledateformat ("yyy-mm-dd hh: mm: SS "). format (mydate); system. out. println (runtime + "scheduled task is being executed ......... ");}}
2. Set to call simple tasks
Package COM. boonya. timer; import Java. util. timer;/*** runmytask. java * function: call scheduled tasks * @ author boonya * @ version 1.0 2012-10-11 */public class runmytask {public static void execute () {timer = new timer (); // execute once a day/* Date start = new date (); start. setdate (start. getdate () + 1); start. sethours (2); start. setminutes (0); start. setseconds (0); long l = start. gettime ()-system. currenttimemillis (); timer. schedule (New mytask (), L, 24*1000*60*60); * // The timer that runs once every minute. schedule (New mytask (), 60000, 60*1000); // task, delay, peroid }}
3. Interface for compiling the range of complex task scheduling policies
public interface SchedulerService { void schedule(TimerTask task, long delay); void schedule(TimerTask task, long delay, long period); void schedule(TimerTask task, Date firstTime, long period); void scheduleAtFixedRate(TimerTask task, long delay, long period); void scheduleAtFixedRate(TimerTask task, Date firstTime, long period); }
4. Implement the task policy interface
Public class schedulerserviceimpl implements schedulerservice {// how long will the call be delayed? The thread waits @ override public void schedule (timertask task, long delay) {New timer (). schedule (task, delay);} // call delayed delay in microseconds, and execute it once every microsecond in the future @ override public void schedule (timertask task, long delay, long period) {New timer (). schedule (task, delay, period);} // start from firsttime and run once every second of period @ override public void schedule (timertask task, date firsttime, long period) {New timer (). scheduleatfixedrate (task, firsttime, period);} // call delayed delay once in microseconds and then run it every microsecond in the period. The method focuses on the execution frequency @ override public void scheduleatfixedrate (timertask task, long delay, long period) {New timer (). scheduleatfixedrate (task, delay, period);} // The execution starts from firsttime and runs at intervals of microseconds. The method focuses on the execution frequency @ override public void scheduleatfixedrate (timertask task, date firsttime, long Period) {New timer (). scheduleatfixedrate (task, firsttime, period );}}
Note: We recommend that you only use the schedule method.
5. scheduling service calls
public static void main(String[] args) { SchedulerService service=new SchedulerServiceImpl(); // execute one time then the thread turn to wait status service.schedule(new TemplateTask(), 5*1000); //start from now every 5 seconds execute my task // service.schedule(new TemplateTask(), new Date(), 5*1000); //delay 5 seconds then as 5 seconds time-step to execute my task // service.schedule(new TemplateTask(), 5*1000, 5*1000); // service.scheduleAtFixedRate(new TemplateTask(), new Date(), 5*1000); // service.scheduleAtFixedRate(new TemplateTask(), 5*1000, 5*1000); }
6. Start and execute tasks in the Web configuration project
--- You can set a servlet to start a scheduled task when the project starts. The web. xml configuration is as follows:
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ReadUserListHandler</servlet-name> <servlet-class>com.boonya.test.servlet.ReadUserListHandler</servlet-class> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ReadUserListHandler</servlet-name> <url-pattern>/servlet/ReadUserListHandler</url-pattern> </servlet-mapping>
Note: <load-on-startup> note,
In servlet configuration, <load-on-startup> 5 </load-on-startup> indicates:
Mark whether the servlet is loaded when the container is started.
When the value is 0 or greater than 0, the servlet is loaded when the application is started;
When it is a negative number or when it is not specified, it indicates that the container is loaded only when the servlet is selected.
The smaller the positive value, the higher the priority of starting the servlet.
7. Notes
Each timer corresponds to only one thread.
Timer does not guarantee that the task execution is very accurate.
Timer has thread security risks.